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
59 changes: 56 additions & 3 deletions scripts/pr_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,45 @@ def reviewed_head(pr: dict) -> bool:
return bool(head_reviews(pr))


def head_review_done(pr: dict, min_rounds: int) -> bool:
"""True once a round past position `min_rounds` in the reviewer's own history covers the head.

Positions are read off `reviewer_nodes(pr, "reviews")`, every one of the reviewer's own
reviews on this pull request, on any head, refusals included, oldest first: the exact list
`status` counts for its own `rounds=`. A caller that already read that count, then
re-requested a review on an already-covered head, passes it through `wait --min-rounds`
instead, so this holds out for a round sitting past it.

The round satisfying `min_rounds` and the round satisfying coverage must be the same round,
which is why this walks one list rather than checking `reviewed_head(pr)` and a count as two
independent clauses. Two clauses would let a refusal past the baseline satisfy the count
while an untouched, older genuine round still satisfies `reviewed_head`, reporting done on a
round that answered the re-request by declining it. A caller passing 0, every caller that
predates this flag among them, reduces to `reviewed_head` exactly, since position 0 is every
position a non-empty list has.

Counting `reviewer_nodes(pr, "reviews")` rather than `head_reviews(pr)`, the head-scoped,
refusal-excluded list `reviewed_head` itself narrows to, matters independently of the above:
a push during the wait moves the head, which empties a head-scoped list and reads a brand
new head's first genuine round as still short of a baseline measured against a head that no
longer exists, where this list carries forward since a push adds to it rather than resetting
it. Positions still land past `min_rounds` correctly once the head moves, since a review on
the superseded head sits at an earlier position than one on the new head, never a later one.

The same `last:100` window both `Q_LIVE` and `Q_FULL` place on their own `reviews`
connection bounds this the way it bounds `status`'s own `rounds=`: a pull request whose
Copilot rounds have aged out of that window under other reviewers' own traffic undercounts
here exactly as `status` would, reading a landed round as still short of the baseline rather
than crediting it, so this fails toward a caller waiting longer, not toward a false success.
"""
revs = reviewer_nodes(pr, "reviews")
head = pr["headRefOid"]
return any(
i >= min_rounds and (n.get("commit") or {}).get("oid") == head and not refusal_of(n)
for i, n in enumerate(revs)
)


def review_effort(pr: dict) -> tuple[str, str]:
"""The newest head review's effective effort and selection source.

Expand Down Expand Up @@ -3444,6 +3483,18 @@ def main(argv: list[str] | None = None) -> int:
"activity elsewhere in this repository is a quota-limit refusal with nothing "
"answering it since, pass this once the quota is believed to have reset",
)
ap.add_argument(
"--min-rounds",
type=int,
default=0,
metavar="N",
help="wait: treat the head as covered only once the reviewer's total rounds on this "
"pull request, the same count `status` prints as `rounds=`, exceed N. Pass the "
"`rounds=` a prior `status` already read, after re-requesting a review on a head that "
"already carries one: `reviewed_head` alone would report that earlier round as done "
"at once, never waiting for the fresh one the re-request was for (default 0, so any "
"round counts, unchanged from before this flag existed)",
)
# `reply` takes the finding's words rather than its id.
# There is deliberately no argument an id fits in, so the caller never holds one to mistype.
ap.add_argument(
Expand Down Expand Up @@ -3493,6 +3544,8 @@ def main(argv: list[str] | None = None) -> int:
)
if a.pickup_grace < 0:
ap.error("--pickup-grace cannot be negative")
if a.min_rounds < 0:
ap.error("--min-rounds cannot be negative")
# A negative threshold reports every check in that state, on every run, from the first read.
# A field that fires always is one a reader learns to skip, which costs the real case.
for name in ("check_grace", "check_stall"):
Expand Down Expand Up @@ -3540,7 +3593,7 @@ def main(argv: list[str] | None = None) -> int:
delays = [15, 20, 30, 45, 60, 120]
start = time.monotonic()
pr = gql(Q_LIVE, owner, repo, a.number)
done, answer = reviewed_head(pr), answered_outside_review(pr)
done, answer = head_review_done(pr, a.min_rounds), answered_outside_review(pr)
# A drifted login matches no filter here, so `done` stays false however long this runs.
# Waiting it out reports a review that landed as one that never did, at the timeout.
# The liveness query carries the authors, so this costs the loop no extra call.
Expand Down Expand Up @@ -3583,7 +3636,7 @@ def main(argv: list[str] | None = None) -> int:
i += 1
# Re-read head each iteration: a push during the wait moves it.
pr = gql(Q_LIVE, owner, repo, a.number)
done, answer = reviewed_head(pr), answered_outside_review(pr)
done, answer = head_review_done(pr, a.min_rounds), answered_outside_review(pr)
drift = reviewer_login_drift(pr)

# One payload decides the digest and the exit code together.
Expand Down Expand Up @@ -3616,7 +3669,7 @@ def main(argv: list[str] | None = None) -> int:
# Gating the verdict behind it left the login check unable to reach an exit code.
# The digest above printed `shapes=UNRECOGNIZED` the whole time it did so.
# Coverage of the head is the other half, returning 0 only once the diff is covered too.
if unrecognized_shapes(final) or reviewed_head(final):
if unrecognized_shapes(final) or head_review_done(final, a.min_rounds):
verdict = report_verdict(final, owner, repo)
# The check reading ranks under both of those, and never replaces either.
# An unreadable shape means no field here can be believed, this one included.
Expand Down
112 changes: 112 additions & 0 deletions tests/test_pr_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -4430,6 +4430,112 @@ def test_wait_polls_again_after_a_pending_round(self) -> None:
self.assertEqual(0, self.cli(["wait", "7"]))
self.assertEqual(1, slept.call_count)

def test_min_rounds_holds_out_past_a_round_status_already_reported(self) -> None:
"""`status`'s guidance for unstated coverage is to re-request on the same head, which
leaves an earlier round covering it while the fresh one this wait was called to watch
for is still outstanding. Naming that earlier `rounds=` count on `--min-rounds` is what
`wait` needs to hold out for one beyond it, rather than reading `reviewed_head` alone and
reporting the earlier round as done at once without ever waiting for the fresh one the
re-request was for. The first poll here carries that shape: one review already on the
head, a request still pending. Without the flag,
`wait` returns on it unpolled, per `test_wait_returns_zero_once_the_review_lands_on_the_head`.
"""
self.answer(
payload([review()], pending=True),
payload([review(), review(rid="PRR_two", at=LATE)]),
)
with mock.patch.object(pr_review.time, "sleep") as slept:
self.assertEqual(0, self.cli(["wait", "7", "--min-rounds", "1"]))
slept.assert_called_once()

def test_min_rounds_times_out_rather_than_crediting_the_round_it_was_told_to_skip(self) -> None:
"""A round no higher than `--min-rounds` must not report success at the timeout either,
or the exit code repeats the bug the polling half of this fix already closes."""
self.answer(payload([review()], pending=True))
with mock.patch.object(pr_review.time, "sleep"):
self.assertEqual(30, self.cli(["wait", "7", "--min-rounds", "1", "--timeout", "0"]))
self.assertIn("status=PENDING", self.out.getvalue())

def test_min_rounds_counts_a_stale_off_head_round_the_way_status_does(self) -> None:
"""`min_rounds` must be the same count `status` prints as `rounds=`: every one of the
reviewer's own reviews on this pull request, on any head. A count narrowed to the
current head disagrees with that whenever a stale, off-head round sits in the history,
and undercounts every round after it forever, since the off-head round it excludes never
ages out. Two rounds already exist here, one off the current head, matching a `status`
read of `rounds=2` before the re-request. The one that lands after it is the third, on
the head, and is what `--min-rounds 2` was asked to hold out for.
"""
self.answer(
payload([review(oid=OLD, rid="PRR_stale"), review(rid="PRR_head_one")], pending=True),
payload(
[
review(oid=OLD, rid="PRR_stale"),
review(rid="PRR_head_one"),
review(rid="PRR_head_two", at=LATE),
]
),
)
with mock.patch.object(pr_review.time, "sleep") as slept:
self.assertEqual(0, self.cli(["wait", "7", "--min-rounds", "2"]))
slept.assert_called_once()

def test_min_rounds_counts_a_head_refusal_the_way_status_does(self) -> None:
"""A refusal sits in `status`'s `rounds=` but not in a head-scoped count, since a refusal
covers nothing and `reviewed_head` excludes it. A caller reading `rounds=1` off one
refusal and reaching `--min-rounds 1` once a genuine round then lands beside it must see
that genuine round as the fresh one it asked for, not read the head-scoped count, still 1
because it never counted the refusal to begin with, as still short of the baseline.

The first poll is bodyless, the shape the liveness query itself carries: `refusal_of`
reads nothing there and counts the refusal as ordinary coverage, same as
`test_the_liveness_reading_ends_the_wait_and_the_full_read_refuses_it` above. The full
read that follows carries the body and is where the refusal is actually read as one,
alongside the genuine round that answers it.
"""
bodyless_refusal = {k: v for k, v in review(rid="PRR_refusal").items() if k != "body"}
self.answer(
payload([bodyless_refusal], pending=True),
payload([review(body=REFUSED, rid="PRR_refusal"), review(rid="PRR_genuine", at=LATE)]),
)
with mock.patch.object(pr_review.time, "sleep") as slept:
self.assertEqual(0, self.cli(["wait", "7", "--min-rounds", "1"]))
slept.assert_called_once()

def test_min_rounds_does_not_credit_a_refusal_as_the_fresh_round(self) -> None:
"""The round satisfying `min_rounds` and the round satisfying coverage must be the same
round, or a refusal answering the re-request reads as done off an untouched, older
genuine round instead. One genuine round already sits on the head, matching a `status`
read of `rounds=1`. The caller re-requests and runs `wait --min-rounds 1`, and the round
that comes back is a refusal, not the fresh genuine round the re-request was for.
Checking `reviewed_head` and the count as two independent clauses would read this as
done, since the untouched round0 still satisfies `reviewed_head` and the refusal at
round1 still pushes the count past 1, returning 0 on a round that declined. Walking one
list for a single round satisfying both instead falls through to the refusal this newest
round actually is, which is the correct, narrower failure `refusing_review` names.
"""
self.answer(payload([review(rid="PRR_one"), review(body=REFUSED, rid="PRR_ref", at=LATE)]))
with mock.patch.object(pr_review.time, "sleep"):
self.assertEqual(41, self.cli(["wait", "7", "--min-rounds", "1", "--timeout", "0"]))
self.assertIn("status=REVIEW_IS_A_REFUSAL", self.out.getvalue())

def test_min_rounds_survives_a_push_moving_the_head(self) -> None:
"""A head-scoped count resets to zero the moment a push moves the head, so it reads a
brand new head's first genuine round as still short of a baseline that was measured
against a head that no longer exists. The unscoped count `status` prints never resets:
one round already sits on the old head, matching a `status` read of `rounds=1`, and the
round that lands on the head after the push is the second, which is what
`--min-rounds 1` was asked to hold out for.
"""
stale = payload([review(oid=OLD, rid="PRR_stale")], pending=True)
stale["headRefOid"] = OLD
stale["commits"]["nodes"][0]["commit"]["oid"] = OLD
self.answer(
stale, payload([review(oid=OLD, rid="PRR_stale"), review(rid="PRR_two", at=LATE)])
)
with mock.patch.object(pr_review.time, "sleep") as slept:
self.assertEqual(0, self.cli(["wait", "7", "--min-rounds", "1"]))
slept.assert_called_once()

def test_wait_exits_thirty_at_the_timeout_rather_than_reporting_success(self) -> None:
"""Pending is not failure and not success, so it takes a code of its own."""
self.answer(payload([review(oid=OLD)]))
Expand Down Expand Up @@ -5822,6 +5928,12 @@ def test_a_negative_pickup_grace_is_rejected_rather_than_read_as_every_poll(self
# The repository is named, or this exits on the missing argument and proves nothing.
self.assertIn("pickup-grace", err.getvalue())

def test_a_negative_min_rounds_is_rejected_rather_than_read_as_any_round(self) -> None:
"""A negative count is satisfied by the same `rounds=0` a caller naming none would see."""
with contextlib.redirect_stderr(io.StringIO()) as err, self.assertRaises(SystemExit):
pr_review.main(["wait", "7", "--repo", "o/r", "--min-rounds", "-1"])
self.assertIn("min-rounds", err.getvalue())

def test_a_negative_check_threshold_is_rejected_rather_than_firing_on_every_check(self) -> None:
"""Below zero, every check in that state reports stuck from the first read.

Expand Down
Loading