From 68f4f549f47beb3d7506b56fbb46eb4db6919218 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:05:16 +0100 Subject: [PATCH 1/2] Poll source checks closer to the wait deadline Architecture-Review: unchanged; the final source observation uses its existing read deadline and preserves zero-wait behavior. Co-Authored-By: Codex --- src/loopzero/cli.py | 26 +++++++++++++++++++------- tests/test_cli.py | 22 +++++++++++----------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index f1645fb8..0c59b059 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -1306,17 +1306,25 @@ 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, initial_read_time: float = 0.0, +) -> Iterator[None]: delay = WAIT_INTERVAL + longest_read = initial_read_time while True: + read_started = time.monotonic() yield # Observe immediately, including --wait=0. - remaining = timeout - (time.monotonic() - started) + now = time.monotonic() + longest_read = max(longest_read, now - read_started) + remaining = timeout - (now - started) if remaining <= 0: return 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. - _sleep(remaining / 2) + # Poll late enough to see a settled check while reserving time to read it. + baseline = min(5.0, max(0.5, remaining / 10)) + allowance = min(remaining / 2, max(baseline, 2 * longest_read)) + _sleep(remaining - allowance) yield remaining = timeout - (time.monotonic() - started) if remaining > 0: @@ -1330,6 +1338,7 @@ def _wait_with_deadline( wt: Path, config: Config, pr: github.PR, timeout: float, *, kicked: bool, started: float | None = None, shown: tuple[str, ...] = (), args: argparse.Namespace, allow_merged: bool = False, allow_skipped: bool = False, initial: github.Readiness | None = None, + initial_read_time: float = 0.0, ) -> tuple[github.PR, github.Readiness | None, bool] | None: started = time.monotonic() if started is None else started if timeout == 0: @@ -1345,7 +1354,7 @@ def _wait_with_deadline( with github.bounded_reads(deadline, token=None, retry=True): waited = _wait_for_checks(wt, config, pr, timeout, kicked=kicked, started=started, shown=shown, args=args, bounded=True, allow_merged=allow_merged, - allow_skipped=allow_skipped, initial=initial) + allow_skipped=allow_skipped, initial=initial, initial_read_time=initial_read_time) reported_timeout = waited is None except github.ReadDeadline: if not reported_timeout: @@ -1389,6 +1398,7 @@ def _wait_for_checks( started: float | None = None, shown: tuple[str, ...] = (), args: argparse.Namespace | None = None, bounded: bool = False, allow_merged: bool = False, allow_skipped: bool = False, initial: github.Readiness | None = None, + initial_read_time: float = 0.0, ) -> tuple[github.PR, github.Readiness | None, bool] | None: """Poll the same head until it settles, omitting readiness for an allowed merge.""" grace_started = time.monotonic() @@ -1396,7 +1406,7 @@ def _wait_for_checks( current = pr stale_notice = False previous = config - for _ in _poll_until(started, timeout, bounded=bounded): + for _ in _poll_until(started, timeout, bounded=bounded, initial_read_time=initial_read_time): if initial is not None: readiness = initial initial = None @@ -1642,6 +1652,7 @@ def cmd_merge(args: argparse.Namespace) -> int: mergify.dequeue(config.repo, pr.number) print(f"requested removal of PR #{pr.number} from Mergify") return 0 + read_started = time.monotonic() pr, readiness = _pr_readiness( wt, config, allow_merged=True, workflow_wait=args.wait is not None ) @@ -1653,7 +1664,8 @@ def cmd_merge(args: argparse.Namespace) -> int: # One waiter and one deadline span source checks and landing: a check created # by the ready transition must not end the turn between `ready` and the queue. waited = _wait_with_deadline(wt, config, pr, args.wait, kicked=False, started=started, - args=args, allow_merged=True, initial=readiness) + args=args, allow_merged=True, initial=readiness, + initial_read_time=started - read_started) if waited is None: return 3 pr, readiness, _ = waited diff --git a/tests/test_cli.py b/tests/test_cli.py index 5c136d5d..6df10da4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2435,9 +2435,7 @@ def get(_repo, path): @pytest.mark.parametrize("command", ["ready", "merge"]) -def test_source_wait_reads_once_before_first_interval( - wt, gh, capsys, monkeypatch, source_queue_api, command, -): +def test_source_wait_reads_once_before_first_interval(wt, gh, capsys, monkeypatch, source_queue_api, command): head = commit_mergify_configuration(wt) arm_approved_pr_with_check_result(gh, head, wt, "pending", isDraft=False) repo, config_path, vendor_calls = source_queue_api @@ -2446,15 +2444,16 @@ def test_source_wait_reads_once_before_first_interval( def after_sleep(): observations.append(([call["argv"][1] for call in gh.calls], len(vendor_calls))) - arm_check_runs(gh, head, [{"name": "checks", "conclusion": "success"}]) + if clock.now >= 6: + arm_check_runs(gh, head, [{"name": "checks", "conclusion": "success"}]) clock = poll_clock(monkeypatch, jitter=1.0, after_sleep=after_sleep) monkeypatch.setattr(mergify, "markers", lambda *_: (True, False)) monkeypatch.setattr(mergify, "membership", lambda *_: None) monkeypatch.setattr(cli, "_wait_for_mergify", lambda *_: "e" * 40) - _out, _err = expect_cli(capsys, 0, command, "--wait=90", stderr="") - assert clock.now == cli.WAIT_INTERVAL + _out, _err = expect_cli(capsys, 0, command, "--wait=8", stderr="") + assert clock.now == pytest.approx(7.2) [(before_sleep, vendor_count)] = observations assert before_sleep.count(f"{repo}/pulls/7") == 1 assert before_sleep.count(reviews_key().split(" ", 1)[1]) == 1 @@ -2639,8 +2638,8 @@ def advance(): out, _err = expect_cli(capsys, expected, "ready", "--wait=40", stderr="") assert "waiting: required check 'checks' is cancelled" in out - assert clock.sleeps == ([30.0, 5.0, 5.0] if replacement == "pending" else - [30.0, 5.0] if initial_pending else [30.0]) + assert clock.sleeps == ([30.0, 9.0, 1.0] if replacement == "pending" else + [30.0, 9.0] if initial_pending else [30.0]) assert out.endswith(f"ready: {URL}\n" if expected == 0 else f"not ready: required check 'checks' is failure{FAILED_CHECK_HINT}\n" if expected == 1 else f"timed out waiting for required checks on {head[:12]}. Run `loopzero ready --wait` to resume on this head.\n") @@ -2679,7 +2678,7 @@ def late_read(*args, **kwargs): code, out, err = run(capsys, "ready", "--wait=40") assert code == (1 if after in {"head_move", "blocker", "cancelled"} else 3) - assert clock.sleeps == ([30.0, 5.0, 5.0] if after == "pending" else [30.0]) + assert clock.sleeps == ([30.0, 9.0, 1.0] if after == "pending" else [30.0]) assert "ready: https" not in out assert ("PR head changed while waiting" in err) == (after == "head_move") if after == "cancelled": @@ -2713,7 +2712,7 @@ def advance(): assert (code, err) == (0 if run_appears else 3, "") assert marked_at == [210.0] - assert clock.sleeps == [30.0, 60.0, 120.0, 30.0] + ([] if run_appears else [30.0, 30.0]) + assert clock.sleeps == [30.0, 60.0, 120.0, 30.0] + ([] if run_appears else [55.0, 5.0]) assert out.endswith(f"ready: {URL}\n" if run_appears else f"timed out waiting for required checks on {head[:12]}. Run `loopzero ready --wait` to resume on this head.\n") assert sum(c["argv"][:2] == ["pr", "ready"] for c in gh.calls) == 1 @@ -3195,11 +3194,12 @@ def slow_check(argv, *, timeout, **kwargs): clock.now += timeout raise cli._proc.ProcTimeout("gh api timed out", "") initial_check_seen = True + clock.now += 2.0 return run_gh(argv, timeout=timeout, **kwargs) monkeypatch.setattr(github, "run", slow_check) out, _err = expect_cli(capsys, 3, "merge", "--wait=16", stderr="") - assert budgets == pytest.approx([8.0]) + assert budgets == pytest.approx([4.0]) assert f"timed out waiting for required checks on {head[:12]}. Run `loopzero merge --wait` to resume on this head." in out assert all(c["argv"][:2] != ["pr", "merge"] for c in gh.calls) From 71cef5a334b896f291e0335f238a7f63bffae080 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:16:54 +0100 Subject: [PATCH 2/2] Reserve time for the initial ready observation Architecture-Review: unchanged; ready and merge both account for observed read latency within the existing wait deadline. Co-Authored-By: Codex --- src/loopzero/cli.py | 7 +++++-- tests/test_cli.py | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 0c59b059..3e3d177e 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -1215,6 +1215,7 @@ def _cleanup(wt: Path, pr: github.PR) -> None: def cmd_ready(args: argparse.Namespace) -> int: wt, config = _context(args, pin_checks=True) + read_started = time.monotonic() pr, readiness = _pr_readiness(wt, config, workflow_wait=args.wait is not None) assert readiness is not None waiting = _draft_checks_waiting(config, pr, readiness) @@ -1232,7 +1233,8 @@ def cmd_ready(args: argparse.Namespace) -> int: ): waited = _wait_with_deadline(wt, config, pr, args.wait, kicked=kicked, started=started, shown=readiness.reasons if kicked else (), args=args, - allow_skipped=True, initial=None if kicked else readiness) + allow_skipped=True, initial=None if kicked else readiness, + initial_read_time=started - read_started) if waited is None: return 3 pr, readiness, kicked = waited @@ -1241,7 +1243,8 @@ def cmd_ready(args: argparse.Namespace) -> int: github.mark_ready(config.repo, pr.number) if args.wait is not None: waited = _wait_with_deadline(wt, config, pr, args.wait, kicked=True, started=started, - args=args, allow_skipped=True) + args=args, allow_skipped=True, + initial_read_time=started - read_started) if waited is None: return 3 pr, readiness, kicked = waited diff --git a/tests/test_cli.py b/tests/test_cli.py index 6df10da4..3d2b67a5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3178,14 +3178,14 @@ def test_merge_wait_times_out_on_pending_checks_with_exit_three( assert wt.exists() and all(c["argv"][:2] != ["pr", "merge"] for c in gh.calls) -def test_merge_wait_bounds_slow_source_check_read(wt, gh, capsys, monkeypatch, head, replay_gh): +@pytest.mark.parametrize("command", ["ready", "merge"]) +def test_source_wait_bounds_slow_check_read(wt, gh, capsys, monkeypatch, head, replay_gh, command): arm_approved_pr_with_check_result(gh, head, wt, "pending", isDraft=False) budgets = [] initial_check_seen = False run_gh = github.run clock = poll_clock(monkeypatch, start=1000.0) monkeypatch.setattr(github, "_retry_sleep", lambda _: None) - def slow_check(argv, *, timeout, **kwargs): nonlocal initial_check_seen if len(argv) > 2 and argv[2].endswith("/check-runs"): @@ -3198,9 +3198,9 @@ def slow_check(argv, *, timeout, **kwargs): return run_gh(argv, timeout=timeout, **kwargs) monkeypatch.setattr(github, "run", slow_check) - out, _err = expect_cli(capsys, 3, "merge", "--wait=16", stderr="") + out, _err = expect_cli(capsys, 3, command, "--wait=16", stderr="") assert budgets == pytest.approx([4.0]) - assert f"timed out waiting for required checks on {head[:12]}. Run `loopzero merge --wait` to resume on this head." in out + assert f"timed out waiting for required checks on {head[:12]}. Run `loopzero {command} --wait` to resume on this head." in out assert all(c["argv"][:2] != ["pr", "merge"] for c in gh.calls)