Skip to content

[hardened PR review] Declutter replaced reviews: fold, delete unreplied comments, link nearby threads - #201479

Closed
izaitsevfb wants to merge 4 commits into
mainfrom
pr-review-delete-superseded-comments
Closed

izaitsevfb wants to merge 4 commits into
mainfrom
pr-review-delete-superseded-comments

Conversation

@izaitsevfb

@izaitsevfb izaitsevfb commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

minimizing obsolete automatic reviews did not hide their review comments, meaning the identical comments from different review iterations could pile up on the same line.
this PR deletes obsolete comments (that don't have maintainer replies), and also edits the obsolete review to wrap them in <details> so they render better on mobile (which doesn't currently support minimization).


✴️ iz2: On PRs with many re-reviews (e.g. #201405: 24 automated reviews, 67 open inline threads), minimizing a replaced review hides only its body. Its inline comments stay listed on the Files tab and in GitHub Mobile, because GITHUB_TOKEN can't resolve threads here.

When a newer automated review replaces an earlier one, this change:

  • folds the earlier review's body into a collapsed <details> section (the marker stays the first line; no cross-links)
  • deletes its inline comments that nobody replied to or reacted to. A comment is deleted only if its review thread, read by GraphQL cursor, holds that one comment. Answered comments and their replies stay.
  • posts a new blocking finding within 3 lines of an earlier comment someone replied to in the review body, with a link to that thread, instead of opening another inline thread. The match is by position only, so the link text says only that the thread is nearby.

Reviews hidden by earlier runs get the same fold and delete from a backfill. The backfill runs after any clean-verdict dismissal and is capped at 50 writes, 1 second apart, and 90 seconds per run, so a PR like #201405 is cleaned over a few runs. A change request that may still stand is never folded or emptied.

Known limits:

  • A reply that isn't visible yet, because it was posted after the threads were read or is still pending in someone's unsubmitted review, does not protect its root comment. The API shows neither, so a maintainer who is mid-reply when a new push is reviewed can lose the comment they were answering.
  • Throttled deletes on the takedown path, before a clean verdict's dismissal, are not time-bounded.

Test plan: python3 -m unittest discover -s scripts/pr_review -t . passes all 472 tests, including new cases for overlapping runs, reply, reaction and thread-count protection, retraction, and the backfill's budget, pacing, clock and change-request gate.

…ed comments, link discussions

Minimizing a replaced review hides only its body; its inline comments stay
listed on the Files tab and in GitHub Mobile, so a PR with many re-reviews
shows every old finding at once (GITHUB_TOKEN cannot resolve threads here).

When a newer review replaces an earlier one:
- its body is folded into a collapsed <details> section (marker kept first);
- its inline comments nobody replied to are deleted; replied ones and their
  replies stay;
- a new finding next to an earlier comment someone replied to goes in the
  body with a link to that discussion instead of a new inline comment.

Reviews hidden by earlier runs are folded and emptied by a backfill that runs
after any clean-verdict dismissal, capped at 50 writes and 90 seconds a run.
A comment any listing in the run saw replied to is never deleted.
@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Oct 19, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - Ready for human review:

When a newer automated review replaces an older one, this PR folds the older
review's body into a collapsed section, deletes its inline comments that nobody
replied to or reacted to, links new blocking findings near replied threads from
the review body, and backfills the same fold and delete for reviews that earlier
runs already hid. Recommendation: Needs Discussion. The main question is a
design one: deleting unreplied comments right after the next automated review
races with humans who are replying, and the pacing, state, naming,
redundant-read and test gaps should be fixed in the same pass.

Findings (10):

[minor] scripts/pr_review/post_review.py:586
A reply someone is writing can lose the comment it answers, and the timing makes
this likely. `take_down` deletes the previous review's comments within seconds
of the next automated review. A new review usually follows a push, which is
exactly when a human may be partway through replying in a pending review, and
neither `replies_seen` nor `thread_sizes` can see a pending reply. The race can
be narrowed without new API surface: only delete comments of reviews that a
previous run already hid (so the backfill does the deleting), or skip root
comments whose `created_at` is younger than a grace per

[minor] scripts/pr_review/post_review.py:586
Deletes on the takedown path are not paced. `take_down` calls
`delete_unreplied(gh, pr, rid, comments)` with the default `Budget()`, which has
no pause, so up to `MAX_FINDINGS` (25) DELETEs go out back to back, with resolve
mutations right after. The backfill waits `BACKFILL_PAUSE` between writes as
GitHub asks, and that rule applies here too. This path is also where throttled
retries have no time limit. Pass `Budget(pause=BACKFILL_PAUSE)` (or a shared
constant).

[minor] scripts/pr_review/post_review.py:134
Run-wide policy state is kept on the `GitHub` transport object (also lines 136,
146, 202, 750-756). `deadline` is a mutable on/off flag that `backfill` sets and
resets, and `request`/`graphql` read it implicitly. `replies_seen` is the
deletion gate: `pr_comments` adds to it as a side effect, and
`delete_unreplied`/`with_discussions` read it. This is hidden coupling, because
whether a delete is safe depends on which listings ran earlier on the same
client. Make the deadline a context manager (`with
gh.time_limit(BACKFILL_SECONDS):`) and keep the reply set in a run-owned object
passed explicitly

[minor] scripts/pr_review/post_review.py:765
The backfill re-reads everything on every run, even when nothing is left to
tidy. If any hidden review exists that is not a standing request, `_backfill`
lists every inline comment and pages through every review thread, even when all
hidden reviews are already folded and emptied. One run can list comments up to 4
times, page threads up to 3 times, and list reviews 36 times, all against
`GITHUB_TOKEN`'s per-repo rate limit. Record when a review is fully tidied (for
example, a separate fold marker written only once every unreplied comment is
gone) and skip such reviews before listing anything.

[minor] scripts/pr_review/post_review.py:604
The new module-level `folded()` is shadowed by `review_body`'s local `folded =
[...]` (line 318) and by `_compose`'s `folded` parameter (line 339), both of
which mean non-blocking findings. "Folded" now also names two different things:
`FOLDED_MARK` (non-blocking findings) and `FOLDED_REVIEW` (an outdated review).
Rename the new pieces, for example `outdated_body()`, `fold`
`collapse_outdated`, and `OUTDATED_REVIEW`.

[minor] scripts/pr_review/post_review.py:491
`ours()` is a fourth copy of the "is this our review" check. It repeats the
author + `MARKER` test already in `earlier_reviews`, `standing_requests` and
`latest_is_request`. The copies have already drifted: two check
`isinstance(r.get("id"), int)` and two don't. Pull out a single
`_is_ours(review, author)` helper.

(4 more not shown, see the review run)

Reviewed commit: 194252b

Review run

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/201479

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 194252b with merge base e5e5dde (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - PR requires human review:

  • Triviality
    • Outside every trivial class: take_down and the new backfill now PUT-rewrite earlier review bodies and DELETE their inline comments
    • Adds new machinery to scripts/pr_review/post_review.py: Budget, OutOfTime, a GitHub.deadline clock, with_discussions position matching
  • Socialization
    • Module docstring invariant changes from nothing being edited or deleted to folding bodies and deleting unreplied comments
    • PR body states a known limit: a maintainer mid-reply can lose the comment they were answering
  • CI plumbing
    • post_review.py runs in .github/workflows/hardened-pr-review-run.yml with the repo token, now issuing irreversible deletes on every PR it reviews
    • Deletion gates (replies_seen, reacted, thread_sizes) and NEARBY = 3 are design choices a human should approve
  • Scope
    • About 500 lines of new logic across retries, posting, hiding, and a capped backfill (MAX_BACKFILL, BACKFILL_SECONDS)

reason: not_trivial
Reviewed commit: 194252b

Inference job

🐛 Report a wrong verdict

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 19, 2026
github-actions[bot]

This comment was marked as outdated.

… its clock runs out

Address the automated review: a comment with reactions is kept like one with
replies; running out of time raises OutOfTime, which ends the backfill instead
of failing every remaining call one by one; graphql() retry waits honour the
clock; hide_earlier lists comments only when it has something to take down.
github-actions[bot]

This comment was marked as outdated.

…e backfill's request gate

Address the second automated review: the link to an earlier replied-to thread
now says only that it is near this line, since matching is by position; the
backfill's change-request gate is tested for a newer request and for one
downgraded to a comment; with_discussions skips listing when there are no
findings.
@izaitsevfb izaitsevfb changed the title [hardened PR review] Declutter replaced reviews: fold, delete unreplied comments, link discussions [hardened PR review] Declutter replaced reviews: fold, delete unreplied comments, link nearby threads Oct 19, 2026
github-actions[bot]

This comment was marked as outdated.

…lds; pace the backfill

Address the third automated review: a comment is deleted only if its review
thread, read by GraphQL cursor (which an overlapping run's deletes cannot
shift), holds that one comment; unreadable threads delete nothing. Folded
bodies are defused again, a retry wait past the clock raises OutOfTime, and
backfill writes pause 1 second apart.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of 194252b: nothing blocking; ready for human review.

When a newer automated review replaces an older one, this PR folds the older review's body into a collapsed section, deletes its inline comments that nobody replied to or reacted to, links new blocking findings near replied threads from the review body, and backfills the same fold and delete for reviews that earlier runs already hid. Recommendation: Needs Discussion. The main question is a design one: deleting unreplied comments right after the next automated review races with humans who are replying, and the pacing, state, naming, redundant-read and test gaps should be fixed in the same pass.

⚪ 10 non-blocking findings

kept = delete_unreplied(gh, pr, rid, comments)

A reply someone is writing can lose the comment it answers, and the timing makes this likely. take_down deletes the previous review's comments within seconds of the next automated review. A new review usually follows a push, which is exactly when a human may be partway through replying in a pending review, and neither replies_seen nor thread_sizes can see a pending reply. The race can be narrowed without new API surface: only delete comments of reviews that a previous run already hid (so the backfill does the deleting), or skip root comments whose created_at is younger than a grace per


kept = delete_unreplied(gh, pr, rid, comments)

Deletes on the takedown path are not paced. take_down calls delete_unreplied(gh, pr, rid, comments) with the default Budget(), which has no pause, so up to MAX_FINDINGS (25) DELETEs go out back to back, with resolve mutations right after. The backfill waits BACKFILL_PAUSE between writes as GitHub asks, and that rule applies here too. This path is also where throttled retries have no time limit. Pass Budget(pause=BACKFILL_PAUSE) (or a shared constant).


self.replies_seen: set[int] = set()

Run-wide policy state is kept on the GitHub transport object (also lines 136, 146, 202, 750-756). deadline is a mutable on/off flag that backfill sets and resets, and request/graphql read it implicitly. replies_seen is the deletion gate: pr_comments adds to it as a side effect, and delete_unreplied/with_discussions read it. This is hidden coupling, because whether a delete is safe depends on which listings ran earlier on the same client. Make the deadline a context manager (with gh.time_limit(BACKFILL_SECONDS):) and keep the reply set in a run-owned object passed explicitly


for old in sorted(hidden, key=lambda r: r["id"], reverse=True):

The backfill re-reads everything on every run, even when nothing is left to tidy. If any hidden review exists that is not a standing request, _backfill lists every inline comment and pages through every review thread, even when all hidden reviews are already folded and emptied. One run can list comments up to 4 times, page threads up to 3 times, and list reviews 36 times, all against GITHUB_TOKEN's per-repo rate limit. Record when a review is fully tidied (for example, a separate fold marker written only once every unreplied comment is gone) and skip such reviews before listing anything.


def folded(review: dict, reason: str = SUPERSEDED) -> str:

The new module-level folded() is shadowed by review_body's local folded = \[...\] (line 318) and by _compose's folded parameter (line 339), both of which mean non-blocking findings. "Folded" now also names two different things: FOLDED_MARK (non-blocking findings) and FOLDED_REVIEW (an outdated review). Rename the new pieces, for example outdated_body(), fold collapse_outdated, and OUTDATED_REVIEW.


def ours(gh: GitHub, pr: int, author: str) -> set[int]:

ours() is a fourth copy of the "is this our review" check. It repeats the author + MARKER test already in earlier_reviews, standing_requests and latest_is_request. The copies have already drifted: two check isinstance(r.get("id"), int) and two don't. Pull out a single _is_ours(review, author) helper.


delete_unreplied(gh, pr, old["id"], comments, budget, False, sizes)

Unclear positional arguments: delete_unreplied(gh, pr, old\["id"\], comments, budget, False, sizes) passes a bare False for refresh. Use keyword arguments (refresh=False, sizes=sizes), or make refresh and sizes keyword-only.


"far": self.discussed(line=10),

The NEARBY boundary is never tested. The "far" case puts the finding at line 3 and the comment at line 10, 7 lines apart. Testing a distance of exactly 3 (linked) and 4 (not linked) would pin the &lt;= in note_discussions (post_review.py:532).


def test_it_goes_in_the_body_with_a_link_to_the_discussion(self):

No test covers a non-blocking finding with a discussion link. note_discussions marks every finding, and finding_blocks prints the link inside the collapsed non-blocking section (post_review.py:361), but every discussion test uses major findings.


def latest_is_request(listing: list[dict], author: str) -> bool:

latest_is_request is not tested with an overlapping newer run, where our newest review has an id greater than new_id and is a REQUEST_CHANGES or a dismissed review. Whether an older hidden request may be emptied in the backfill depends on this function (post_review.py:762-767), and this input is not covered.

This review is AI-generated and advisory. Each new automated review replaces the previous one.

@github-actions
github-actions Bot dismissed stale reviews from themself October 19, 2026 22:29

Withdrawn: the automated review of 194252b found nothing blocking.

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Oct 19, 2026
@huydhn

huydhn commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@claude Review this change

@claude

claude Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Claude finished @huydhn's task in 1m 58s —— View job


Needs Discussion: the logic is careful, but take_down deletes inline comments seconds after the next review is posted, which races with humans who are replying. The other findings are smaller: hidden state on the GitHub client, redundant reads on every run, links to resolved threads, and a few test gaps.

Full review

PR Review: #201479

Summary

When a newer automated review replaces an earlier one, this PR folds the earlier review's body, deletes its inline comments that have no replies or reactions, links new blocking findings that sit near replied threads from the review body, and backfills the same cleanup for reviews that are already hidden. No REVIEW.md applies. I reviewed all of post_review.py and the new test classes. I could not run the unit tests here because the job did not have permission to run python3, so I relied on the PR's test plan (472 passing).

Thread Safety

  • scripts/pr_review/post_review.py:586 (the main design question). take_down deletes comments with no replies on the review that was just replaced, right after the new review is posted. A push is exactly when a maintainer is most likely to be partway through a "Start a review" reply. None of replies_seen, reacted or thread_sizes can see a pending reply, and deleting the root likely orphans or loses that draft (low confidence on GitHub's exact behaviour). The PR body lists this as a known limit, but the timing makes it the common case, not a corner case.
    • Replies are not the only references either. A link to #discussion_r… from a top-level PR comment, an email notification, a Slack thread or another issue becomes a 404 after the delete, and nothing here protects it.
    • Suggestion: keep take_down non-destructive (fold, resolve, minimize) and let only the backfill delete. Restrict that to reviews a previous run already hid, or to root comments whose created_at is older than a grace period (for example 24h). Users get the decluttering a run later, but deletes no longer coincide with pushes. Maintainers should decide this explicitly, since the deletes can't be undone and run on every reviewed PR.

Code Quality

  • post_review.py:134-136: run-wide state lives on the transport object. replies_seen is a deletion gate that pr_comments fills as a side effect. delete_unreplied and with_discussions then read it, so whether a delete is safe depends on which listings ran earlier on the same GitHub instance. deadline is an on/off flag that backfill sets and clears in try/finally, and request/graphql read it implicitly. Make the deadline a context manager (with gh.time_limit(BACKFILL_SECONDS):) and pass the reply set as an explicit run-owned object to the functions that need it.
  • post_review.py:518-538: note_discussions ignores whether the earlier thread is resolved. If a human resolved the thread (the conversation is over), a new blocking finding within NEARBY still loses its inline comment and goes into the body pointing at the closed thread. _THREADS already fetches isResolved, so thread_sizes could return it too, and resolved threads could then be excluded from discussed.
  • post_review.py:604: the new module-level folded() is shadowed. review_body's local folded = [...] (line 318) and _compose's folded parameter both mean non-blocking findings. FOLDED_MARK (non-blocking findings) and FOLDED_REVIEW (an outdated review) also use the same word for two different things. Rename the new pieces, for example outdated_body / collapse_outdated / OUTDATED_REVIEW.
  • post_review.py:491: ours() is a fourth copy of the author + MARKER check. The others are in earlier_reviews, standing_requests and latest_is_request, and the copies already disagree on isinstance(r.get("id"), int). Factor them into one _is_ours(review, author).
  • post_review.py:777: delete_unreplied(gh, pr, old["id"], comments, budget, False, sizes) passes a bare False for refresh. Make refresh and sizes keyword-only.

Testing

  • NEARBY is never tested at its boundary. The "far" case is 7 lines away, so add distance 3 (linked) and distance 4 (not linked) cases to pin the <= at line 532.
  • No test covers a non-blocking finding with a discussion link. finding_blocks puts that link inside the collapsed section, but every case in TestADiscussedFindingIsNotRepeatedOnTheLine uses major findings.
  • No test asserts that deletes on the takedown path are paced (see Performance).

Performance

  • post_review.py:586: deletes on the takedown path are neither paced nor time-bounded. take_down calls delete_unreplied(...) with the default Budget(), so up to MAX_FINDINGS DELETEs go out back to back with no pause and no deadline. The BACKFILL_PAUSE reasoning (GitHub asks for about 1s between mutating requests) applies here too.
  • post_review.py:759-777: there are many redundant reads. The backfill lists every inline comment and pages every review thread on every run where any hidden review exists, even when all of them are already folded and emptied. Over one run, the reviews list is fetched by ours, both hide_earlier passes, both standing_requests calls, and _backfill. Comments are listed by with_discussions, hide_earlier, the refresh re-listing and _backfill, and threads are paged once per takedown plus once in the backfill. All of this counts against GITHUB_TOKEN's per-repo rate limit.
    • Fix: write a separate marker into the folded body only once every comment that can be deleted is gone, and skip such reviews before listing anything. Also pass one reviews listing through publish → hide_earlier (earlier_reviews already accepts listing=).

Recommendation

Needs Discussion

The decision to delete inline comments right after the next review is irreversible and runs on every PR, so a maintainer should sign off on it. Deferring deletion to a later run is a cheap way to avoid most of the risk. Fix the remaining items in the same pass.

@huydhn huydhn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The agent mentions a nit race, but I don't think we need to worry about that

@izaitsevfb

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 21, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged ready for review Automated review passed, ready for a final review by a maintainer. topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants