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
10 changes: 8 additions & 2 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,13 +1403,18 @@ def _merge_check_hint(config: Config, pr: github.PR, readiness: github.Readiness
return ""


def _poll_until(started: float, timeout: float, *, bounded: bool = False) -> Iterator[None]:
def _poll_until(
started: float, timeout: float, *, bounded: bool = False, admitted: Callable[[], bool] | None = None,
) -> Iterator[None]:
delay = WAIT_INTERVAL
while True:
was_admitted = admitted is not None and admitted()

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: The admission reset relies on a closure seeing a later rebinding of confirmed

_poll_until gets lambda: confirmed, checks it before each yield and checks it again after the yield to spot the transition. This works because Python closures capture variables, not values. When _wait_for_mergify sets confirmed = True inside the loop body, the second call sees the new value. I traced the unconfirmed replay: sleeps come out as [30, 60, 120] while unconfirmed. At t=210, membership is confirmed and delay resets to 30. The landing read at t=240 then sees the merge. Without the fix the sleeps would be [..., 120, 120], so the test does catch the regression. The steady-state, bounded and deadline paths are unchanged for callers that don't pass admitted.

The weak point is that the coupling is implicit. If someone later moves the confirmation into a helper that returns a new value without rebinding the local confirmed, the reset stops firing and nothing complains. A simpler alternative is to have _poll_until accept a value sent back by the caller (gen.send(...)), or to have the caller signal "reset now" directly. Either one makes the transition explicit and removes the second admitted() call. This is not blocking.

yield # Observe immediately, including --wait=0.
remaining = timeout - (time.monotonic() - started)
if remaining <= 0:
return
if admitted is not None and not was_admitted and admitted():
delay = WAIT_INTERVAL
pause = delay * random.uniform(0.9, 1.0)
if bounded and remaining <= pause:
# Leave part of the final interval for the last observation to complete.
Expand Down Expand Up @@ -1664,7 +1669,8 @@ def _wait_for_mergify(
state = None
deferred_landing_read = False
print(f"PR #{pr.number} following Mergify queue {queue}; head {pr.head_sha[:12]}; waiting up to {timeout:g}s")
for _ in _poll_until(started, timeout):
polls = _poll_until(started, timeout, admitted=lambda: confirmed)

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: The polls binding changes nothing and nothing else uses it

for _ in _poll_until(...) already builds the generator once, before the loop starts, so naming it first does not change behavior. The admitted=lambda: confirmed closure reads the same confirmed variable either way, and _poll_until (cli.py:1310/1315) still checks it before and after each yield. polls is not read again in _wait_for_mergify, so there is no send/close or second use that would need the name. The change adds a line and a variable, and the commit title suggests an ordering fix that the code does not need. Smallest fix: revert to the one-line for _ in _poll_until(started, timeout, admitted=lambda: confirmed):. If there is a reason to keep it, say so in a comment.

for _ in polls:
sha = _landed(config, pr, closed)
if sha:
return sha
Expand Down
30 changes: 15 additions & 15 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4338,7 +4338,8 @@ def test_ready_pins_review_publishers_to_base(wt, repo, gh, capsys, monkeypatch,
assert json.loads(gh.calls[-1]["--input"])["state"] == ("success" if publisher == LOGIN else "pending")


def test_mergify_confirmed_wait_reads_pr_once_per_steady_poll(wt, gh, monkeypatch):
@pytest.mark.parametrize("confirmed", [True, False])
def test_mergify_confirmed_wait_reads_pr_once_per_steady_poll(wt, gh, monkeypatch, confirmed):
head = commit_mergify_configuration(wt)
config = cli.config_mod.load_trusted(wt)
pr = github.PR(7, URL, head, "main", False, "OPEN", "MERGEABLE")
Expand All @@ -4350,22 +4351,21 @@ def test_mergify_confirmed_wait_reads_pr_once_per_steady_poll(wt, gh, monkeypatc

def membership(*_):
reads_at_membership.append(len(gh.calls))
if clock.now == 60:
if confirmed and clock.now == 60:
gh.respond(" ".join(detail), merge_json(state="MERGED", head=head, sha="e" * 40))
return mergify.Membership()

def configured(*args):
configurations.append(args)
return True
return mergify.Membership() if confirmed or clock.now >= 210 else None

monkeypatch.setattr(mergify, "configured", configured)
monkeypatch.setattr(mergify, "configured", lambda *args: configurations.append(args) or True)
monkeypatch.setattr(mergify, "mark_confirmed", lambda *_: gh.respond(
" ".join(detail), merge_json(state="MERGED", head=head, sha="e" * 40)))
monkeypatch.setattr(mergify, "membership", membership)

assert cli._wait_for_mergify(config, pr, 60, confirmed=True) == "e" * 40
assert clock.sleeps == [30, 30]
assert configurations == [(REPO, "main", "main")] * 3
assert reads_at_membership == [1, 2, 3]
assert [c["argv"][:2] for c in gh.calls] == [detail] * 4
assert cli._wait_for_mergify(config, pr, 60 if confirmed else 360, confirmed=confirmed) == "e" * 40
assert clock.sleeps == ([30, 30] if confirmed else [30, 60, 120, 30])
assert configurations == [(REPO, "main", "main")] * (3 if confirmed else 4)
assert reads_at_membership == ([1, 2, 3] if confirmed else [1, 4, 7, 10])
assert sum(c["argv"][:2] == detail for c in gh.calls) == (4 if confirmed else 9)
assert len(gh.calls) == (4 if confirmed else 12)


@pytest.mark.parametrize("outcome,jitter", [
Expand Down Expand Up @@ -4438,9 +4438,9 @@ def membership(*_):
if outcome == "worktree_removed":
poll_until = cli._poll_until

def remove_worktree_then_poll(*args):
def remove_worktree_then_poll(*args, **kwargs):
git(repo, "worktree", "remove", "--force", str(wt))
return poll_until(*args)
return poll_until(*args, **kwargs)

monkeypatch.setattr(cli, "_poll_until", remove_worktree_then_poll)
try:
Expand Down
Loading