Skip to content
Merged
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
9 changes: 7 additions & 2 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,18 +1407,22 @@ def _merge_check_hint(config: Config, pr: github.PR, readiness: github.Readiness


def _poll_until(
started: float, timeout: float, *, bounded: bool = False, initial_read_time: float = 0.0,
started: float, timeout: float, *, bounded: bool = False,
initial_read_time: float = 0.0, admitted: Callable[[], bool] | None = None,
) -> Iterator[None]:
delay = WAIT_INTERVAL
longest_read = initial_read_time
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: Admission is checked twice per tick through a closure

_poll_until calls admitted() before and after each yield to detect the transition. This works because the lambda reads the reassigned confirmed local of _wait_for_mergify. However, the reset depends on a side effect inside the loop body, and the generic poller now carries Mergify-specific state. A one-line comment at the call site would help, saying the lambda deliberately reads the live confirmed. Another option is to have the loop body tell the generator to reset, for example with polls.send(...). That would remove the double evaluation. The current code is correct and every existing _poll_until caller and monkeypatch still works: line 1545 passes bounded=..., line 1611 uses positional arguments only, and the lambda *_ stub at tests/test_cli.py:4752 is only used through _wait_for_merge.

read_started = time.monotonic()
yield # Observe immediately, including --wait=0.
now = time.monotonic()
longest_read = max(longest_read, now - read_started)
remaining = timeout - (now - 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:
# Poll late enough to see a settled check while reserving time to read it.
Expand Down Expand Up @@ -1676,7 +1680,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: Unneeded polls temporary

The polls variable is only used once, in the for loop on the next line, and nothing else reads the generator. Calling _poll_until(...) directly in the for statement, as before, keeps the diff smaller and makes it clearer that nothing else uses the generator.

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 @@ -4369,7 +4369,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 @@ -4381,22 +4382,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 @@ -4469,9 +4469,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