Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ loading the delivery [commands](../src/loopzero/cli.py), which implement the con
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
(unresolved threads a review publisher opened with a head-bound marker line),
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer location src/loopzero/cli.py:1253 is not in the diff; anchored here.

important: Bound PR discovery before checking the receipt

Both ready and merge --wait call _require_pr before reaching the new receipt check. If the remote branch exists but its commit has no matching open PR association, pr_for_branch reads the branch, commit associations, the open PR list, and PR detail. That takes four REST requests even when the receipt is missing. A full page of commit associations also triggers another request. The new merge replay uses arm_pr, which returns 404 for the branch lookup and skips the association path. Use a bounded head-filtered PR lookup for these commands, retaining detail validation and the closed-PR fallback for merged cleanup. Cover the existing-branch fallback and pagination cases in the request-budget tests.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer location src/loopzero/cli.py:290 is not in the diff; anchored here.

important: Preserve the receipt check on every readiness poll

_wait_for_checks calls _readiness directly on subsequent polls for both ready --wait and merge --wait. If another check replaces a passing receipt with a failing one while the command waits, the next poll now fetches reviews, CI, and potentially Mergify configuration before returning the receipt failure. A remote error can also mask the repair diagnostic. Restore the receipt check immediately after hold validation when reviews is None. Status supplies reviews explicitly and can still collect all diagnostics.

after checking the pushed head and merge-authority hold. Status still gathers
remote gates for its diagnostics. 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 without recording raw
Expand Down
18 changes: 9 additions & 9 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -311,12 +311,9 @@ def _readiness(
)
report = _load_report(wt)
receipt_problem = _check_report_problem(report, wt, config, head)
if receipt_problem:
result = github.Readiness(
ready=False,
reasons=(*result.reasons, receipt_problem),
)
elif announce_receipt:
result = (github.Readiness(False, (*result.reasons, receipt_problem))
if receipt_problem else result)
if announce_receipt and not receipt_problem:
_print_receipt(wt, report, "accepted")
if config.merge_strategy == "mergify" and pr.state == "OPEN":
behind = github.behind_reason(pr, head if review else None)
Expand Down Expand Up @@ -1250,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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important: Keep receipt read failures from masking holds and merged cleanup

Reading the receipt before _require_pr makes PR diagnostics and merged cleanup depend on the local receipt being readable. For example, if the receipt path is a directory, _load_report raises IsADirectoryError before ready can report a hold or merge can clean up an already merged PR. Use a bounded PR lookup first, validate the pushed head, and handle merged and held PRs before opening the receipt.

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)

Expand Down
4 changes: 2 additions & 2 deletions src/loopzero/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important: Include REST retries in the three-request limit

list_only bounds the number of endpoints, but _gh retries each GET independently. With a missing receipt and an open PR, one transient 503 on the list request and one on the detail request produce four REST requests even when both retries succeed. Apply a shared three-attempt budget or disable retries for this bounded lookup, and cover a transient response in the request-count replay.

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"]))
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3094,13 +3094,26 @@ 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[1:]
if draft:
assert "receipt accepted:" not in out
if condition in {"legacy", "incompatible"}:
assert ("Run loopzero check in this worktree at this head with the same current CLI and effective "
"settings as ready, using a local coordinator for delegated stages." in out)


@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[1:]
assert all("--method" not in call["argv"] and "--input" not in call["argv"] for call in gh.calls)


@pytest.fixture
def check_environment(wt, gh, head, tmp_path):
arm_approved_ready_pr(gh, head, wt, readiness={"local_exit": None}, isDraft=False)
Expand Down
Loading