Skip to content

Serialize Mergify queue requests in a task worktree - #1806

Closed
eowca wants to merge 10 commits into
mainfrom
lean178/1327-mergify-request-r51
Closed

eowca wants to merge 10 commits into
mainfrom
lean178/1327-mergify-request-r51

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Serialize Mergify queue requests in a task worktree

Context and goal

  • Goal: Overlapping merge commands share one Mergify request for the same PR, head and queue.
  • Problem: Both invocations can observe no request marker before either writes its queue comment.
  • Context: Loop Zero already locks task-state updates and records the head and queue in the request comment.

Closes #1327

Supersedes #1428 after resolving the conflict with main's queue budget check. Both review rounds were already used on #1428.

Acceptance

  • Two overlapping invocations share the accepted request even if the POST reply is lost.
  • A failed POST without a marker allows the next invocation to submit.
  • The existing worktree lock covers admission and is released before queue polling.

Notes

The overlapping queue-request tests passed locally. Full validation and queue admission wait for both size budgets to fit.

Base

Base: bb2aae1

Validation

Recorded for d369112a8b58

  • 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

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

Review

  • primary, codex, d04f563, request_changes
  • delta, claude, 93d5730, request_changes

eowca and others added 5 commits November 26, 2026 03:50
…rktree lock

Architecture-Review: unchanged; admission still uses the existing marker and lock and queue waiting starts after the lock is released.
Co-Authored-By: Codex <noreply@openai.com>
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; a new queue request still checks the integrated size before admission.
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 codex on d04f563cad9750aa1b933784eda3e0db34742db4, 53.8s: request_changes (0 critical, 2 important, 1 suggestion)

Same-family fallback: independent reviewers failed: claude: not authenticated. Cross-family review is preferred.

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

  • Call 1: requested model gpt-6.2-astra (effort medium); CLI-reported model gpt-6.2-astra (effort medium); configured model gpt-6.2-astra (effort medium)
Raw reviewer output
Native Codex transcript retained in the restricted local review receipt; public evidence is publisher-derived, not backend attestation. Exact raw SHA256: a7ed64a39b21c60cebc3ad106c3edb1e919c87e9532bdf783cae323d5e01ca19

Comment thread tests/test_cli.py Outdated


@pytest.mark.parametrize("accepted", [True, False])
def test_overlapping_mergify_command_after_lost_reply(wt, gh, monkeypatch, accepted):

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: Both line budgets block queue admission

Running scripts/size_budget.sh bb2aae139ad0dd62c9465558aceab5c0a23fbb2e exited 1. Source is 8,254 lines against an 8,250 cap, and tests are 13,890 against a 13,850 cap. The prequeue check runs this gate before posting the request. Trim at least four source lines and forty test lines while preserving coverage of both accepted and rejected writes.

Comment thread src/loopzero/mergify.py Outdated
github._api(f"repos/{repo}/issues/{number}/comments", {"body": body})
try:
github._api(f"repos/{repo}/issues/{number}/comments", {"body": body})
except (github.GhError, github.ProcTimeout, OSError):

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: Stop after a rate-limited POST

A rate-limited comment POST raises GhUnavailable, which this GhError handler catches. It immediately calls markers(), issuing a GraphQL viewer request and potentially paginating REST comments during the required backoff. A failed read can also hide the POST’s reset or Retry-After guidance. Propagate explicit rate-limit rejections directly and reserve marker reconciliation for failures where the write may have succeeded.

Comment thread tests/test_cli.py
monkeypatch.setattr(github, "_api", transport)
with ThreadPoolExecutor(max_workers=2) as pool:
first = pool.submit(cli.main, ["merge"])
assert writing.wait(10), "first merge did not reach the command POST"

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: Exercise lock release during queue polling

Both invocations use merge without --wait, so the test would still pass if a later change moved _wait_for_mergify inside the lock. Have one invocation enter a paused queue waiter and verify that the other completes admission before polling resumes.

Architecture-Review: unchanged; only ambiguous request failures read the existing queue markers.
Co-Authored-By: Codex <noreply@openai.com>
…request-r51

# Conflicts:
#	src/loopzero/cli.py
#	tests/test_mergify.py
…request-r51

# Conflicts:
#	tests/test_cli.py

@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 93d5730f8c77b9a4cc2169576f274d504f4c8a81, 62.9s: request_changes (0 critical, 1 important, 2 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/mergify.py Outdated
github._api(f"repos/{repo}/issues/{number}/comments", {"body": body})
try:
github._api(f"repos/{repo}/issues/{number}/comments", {"body": body})
except github.GhUnavailable:

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: Catching all of GhUnavailable skips the lost-reply check for 5xx, timeouts and resets, not just rate limits

GhUnavailable is broader than rate limiting. github._api raises it whenever rate_limited or _transient_error(...) is true (github.py:411, 428). That covers HTTP 5xx, "timed out", "connection reset", "unexpected end of JSON input" and "error connecting to api.github.com" (TRANSIENT_RE, github.py:125). Those are exactly the lost-reply failures where GitHub may already have created the queue comment. Before this change, request() re-read the markers and treated an accepted comment as success. Now it re-raises right away, so a 502 or a reset after an accepted POST makes loopzero merge fail even though the request is live. The only reason given for the change was keeping the rate-limit guidance.

The tests don't catch this. test_overlapping_mergify_command_after_lost_reply simulates the lost reply with ProcTimeout, which takes the old path. The new mergify test only covers HTTP 429.

Smallest fix: skip reconciliation only when the error is a rate limit. Alternatively, always reconcile and, if reading the markers fails, re-raise the original error (except ...: raise exc from None) so the guidance is kept. Add a test where a 5xx or connection-reset GhUnavailable comes after an accepted POST.

Comment thread src/loopzero/cli.py
return sha
raise closed[0]
if not confirmed and not requested:
_prequeue_size_budget(wt, config, pr, readiness, project=admit_by is None)

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: Size-budget clone, fetch and script now run while holding the worktree lock

The lock now wraps _prequeue_size_budget (shared clone, fetch from origin, the budget script, and _warn_queue_size fetching predecessor PR refs). It also wraps mergify.membership(..., deadline), which can wait on Retry-After. Any other command that needs lock_local_state (a second merge, check publication, PR-body updates) waits at most 60s (worktree.py:116). After that it fails with TimeoutError: task state is busy instead of sharing the request. Consider running the size budget before taking the lock and re-reading the markers and membership under the lock just before request(). That keeps the lock covering only the check-then-write.

Comment thread tests/test_cli.py
@@ -3879,6 +3879,49 @@ def test_mergify_merge_requests_once_for_behind_head(
else:
assert "queue configuration is unverified" in err

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: Conflict resolution left wrong blank-line spacing

Only one blank line separates test_mergify_merge_requests_once_for_behind_head from the next decorated test (line 3881). Three blank lines follow test_overlapping_mergify_command_after_lost_reply (lines 3923-3925). These are PEP 8 E302/E303 violations left by the merge. Normalize both to two blank lines.

Architecture-Review: unchanged; the GitHub transport distinguishes confirmed rate limits before the existing queue reconciliation.
Co-Authored-By: Codex <noreply@openai.com>
@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Independent Claude review of the fix-only delta from 93d5730f to b85fd33c approved with no new blockers. It verified that a confirmed 429 or rate-limited 403 skips marker reads, while an accepted POST followed by HTTP 502 reconciles successfully. The full slotted check passed on d369112a.

This is the prior fix review record. It is an unregistered approval and does not change this PR's review-round state. The replacement will receive its own registered primary review.

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.

Avoid duplicate Mergify queue comments from overlapping merge retries

2 participants