Skip to content

Keep merge queue polling inside the wait budget - #1780

Draft
eowca wants to merge 16 commits into
mainfrom
lean178/1155-queue-wait-limit
Draft

eowca wants to merge 16 commits into
mainfrom
lean178/1155-queue-wait-limit

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Keep merge queue polling inside the wait budget

Context and goal

  • Goal: Queue observers stop at the existing merge wait deadline.
  • Problem: A short merge wait could accept a late landing or exceed its time budget retrying Mergify.
  • Context: Both backends share the monotonic deadline already used for source checks.

Closes #1155

Acceptance

  • Both queue backends cap reads to the remaining positive budget and admit no further poll or diagnostic read at expiry.
  • A landing verified before the deadline proceeds to cleanup. Late responses and exhausted retries exit 3 with a local timeout message.
  • An explicit zero wait makes one observation.

Notes

Base

main @ 6c58d64

Validation

Recorded for 9ffa612c811e

  • uv run --group dev ruff check src tests: exit 0
  • uv run --group dev tach check: exit 0
  • uv run --python 3.14 python scripts/check_architecture_docs.py: exit 0
  • scripts/size_budget.sh: exit 0
  • uv run --group dev pytest -q: exit 0

Prove it works
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Review

eowca and others added 6 commits November 26, 2026 01:10
Architecture-Review: unchanged; queue admission and landing verification are unchanged while queue I/O now shares the existing wait deadline.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; the deadline applies to observations and leaves the merge request timeout in place.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; the resolution preserves both branches’ CLI tests.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; existing wait semantics now bound queue observations while retaining current merge verification and transport behavior.

Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; synchronous merge verification and cleanup retain their existing contract, with the deadline scoped to queue observation.

Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; the branch preserves main transport behavior and only scopes queue observations.

Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

loopzero primary review by claude on eb3ec183c5e2b111220f4a50ebf5bcfa4d961a69, 111.0s: approve (0 critical, 0 important, 4 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

Comment thread src/loopzero/github.py
sha = merged_sha(repo, number, expected_head=head_sha)
if sha:
# A synchronous merge already in flight still needs verification and cleanup.
if strategy != "queue" and (sha := merged_sha(repo, number, expected_head=head_sha)):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: A direct merge whose first verification read lags past the deadline now exits 3 with a merge-queue message

For squash/merge/rebase, only the first merged_sha read runs outside the read budget. The confirmation that follows it does run inside bounded_reads(admit_by): merge_pending and then the "queue may have landed" re-read in _verify_merge_request. Suppose the synchronous gh pr merge write (300s, unbounded) finishes after the --wait deadline and GitHub's first read still shows the PR as open. The next bounded read then raises ReadDeadline. _merge_and_follow turns that into None, so cmd_merge prints "timed out waiting for the merge queue to land PR #N" and returns 3. Cleanup is skipped even on a repo with no merge queue, and the PR may already be merged. Before this change, the re-read would have verified the merge and cleaned up. The task says a direct merge already in flight keeps its own verification, and this edge partly breaks that. Smallest fix: open the observation budget only when the strategy is queue, or when merge_pending reports the PR as queued. Then the whole non-queue verification sequence stays outside the deadline, as before.

Comment thread src/loopzero/cli.py
return None


def _queue_wait(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: _queue_wait duplicates the bounded-scope and timeout-notice logic of _wait_with_deadline

_queue_wait repeats the pattern from _wait_with_deadline (cli.py:1411-1426) almost line for line. Both open bounded_reads(deadline, token=None, retry=True), track reported_timeout, catch ReadDeadline and print a timeout notice only when the inner wait hasn't already reported one. _queue_wait adds two things: it re-checks the deadline when the poll raises an error, and it keeps a SHA that was verified before the scope check on exit. Because the two copies now differ, they can drift apart. For example, the source-check wait does not convert errors that arrive after the deadline into a timeout, but the queue wait does. Consider having _wait_with_deadline call _queue_wait (or one shared helper), so that both waits get the same rules for late responses.

Comment thread src/loopzero/cli.py Outdated
"""Merge SHA if landed; a closed-unmerged read is held as not final (GitHub lags a
queue merge) and re-raised by the caller only if it persists to the deadline."""
"""Merge SHA if landed; a closed-unmerged read is held as not final."""
github.read_timeout(0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Redundant pre-read deadline checks in _landed

_landed calls github.read_timeout(0) both before and after merged_sha. The call before the read is redundant: merged_sha goes through _gh, which already calls read_timeout(timeout, scoped=read_only) before it starts the subprocess, and that raises ReadDeadline at expiry. Only the check after the read is needed to reject a late response. Removing the first call keeps _landed minimal. The same applies to the explicit check after the read in _verify_merge_request when the next statement is already a bounded read.

Comment thread src/loopzero/mergify.py Outdated
if delay is None:
raise MergifyError("Mergify queue lookup returned no valid JSON") from exc
_retry_sleep(delay)
_retry_sleep(github.read_timeout(delay))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Mergify retry sleeps to the deadline, while the GitHub retry path fails fast

_retry_sleep(github.read_timeout(delay)) sleeps out the rest of the budget when it is shorter than the backoff, and the next _open then raises ReadDeadline. GitHub's _retry_pause does something different: it skips the retry when delay >= read_timeout(limit). Both end in a local timeout, so the result is correct. The behaviours differ, though, and the test locks in sleeps == [1.0]. If you want consistency, reuse the GitHub rule here: when the delay would reach the deadline, skip the sleep and raise ReadDeadline immediately.

eowca and others added 9 commits November 26, 2026 15:27
Architecture-Review: unchanged; both observers keep the existing queue admission, receipt, retry and verification contracts.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; resumed queue observations use the same absolute wait deadline.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; a deadline prevents additional observation without changing admission.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; explicit zero wait still observes once.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; Retry-After and the existing request alarm retain their contracts within the wait budget.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; native queue checks retain exact-head recovery within the observation budget.
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; the local admission lock is released before deadline-bound queue observation.
Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

loopzero delta review by claude on 9ffa612c811ea7c7bf17d0c37d5e4ff04c70cc60, 43.3s: approve (0 critical, 0 important, 3 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

Comment thread src/loopzero/cli.py
confirmed = (deadline is not None and pr.base_ref == config.base_branch
and mergify.membership(config.repo, pr.number, queue) is not None)
and mergify.membership(config.repo, pr.number, queue, deadline if bounded else None) is not None)
except github.ReadDeadline:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Redundant ReadDeadline handler duplicates the one now in cmd_merge

This delta adds except (_AdmissionDeadline, github.ReadDeadline) to cmd_merge (line 1851). That handler prints the same timed out waiting for Mergify to land PR #N and returns 3. _follow_blocked_mergify also catches github.ReadDeadline, prints the same text and returns None, and cmd_merge then turns that None into exit 3 when --wait is set. The two handlers give the same result, so the message and exit path now live in two places that can drift apart. Smallest fix: delete the except github.ReadDeadline branch in _follow_blocked_mergify and let the exception reach cmd_merge. The new regression test would then call through cmd_merge, or assert with pytest.raises(github.ReadDeadline) that no poll follows.

Comment thread src/loopzero/cli.py
except _AdmissionDeadline:
print(f"timed out before requesting Mergify for PR #{pr.number}")
except (_AdmissionDeadline, github.ReadDeadline) as exc:
action = "before requesting Mergify for" if isinstance(exc, _AdmissionDeadline) else "waiting for Mergify to land"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: A ReadDeadline before any Mergify request is reported as 'waiting for Mergify to land'

In _merge_with_mergify, mergify.membership(..., admit_by) at line 1781 runs before mergify.request. If the budget expires during that lookup, the new cmd_merge handler prints timed out waiting for Mergify to land PR #N, even though nothing was requested and the PR may not be in the queue. The admission-path timeout should say timed out before requesting Mergify for PR #N, as _AdmissionDeadline does. Smallest fix: in _merge_with_mergify, convert a ReadDeadline raised inside the pre-request block into _AdmissionDeadline when requested and confirmed are both false, or pick the message from whether a request or confirmation marker exists.

Comment thread src/loopzero/cli.py
sha = None
else:
closed.clear()
github.read_timeout(0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: _landed can discard a verified landing SHA when the read finishes at the deadline

This delta removes the read-budget check before the read in _landed, leaving only github.read_timeout(0) after merged_sha. That check raises ReadDeadline even when merged_sha has just returned a verified SHA. If the landing read finishes as the budget expires, _wait_for_merge and _wait_for_mergify suppress the ReadDeadline. The command then reports a timeout and exits 3 without cleanup, even though the PR landed. This conflicts with the acceptance criterion 'preserve verified landings'. The behaviour predates this delta, but the function was edited here. Smallest fix: return early when a SHA is found (if sha: return sha) before calling github.read_timeout(0), so the post-read check only applies to non-landed observations.

Architecture-Review: unchanged; required CI validation and shared read deadlines both remain in effect.
Co-Authored-By: Codex <noreply@openai.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep merge queue polling inside the existing --wait budget

1 participant