From 3c8e551a13bf0e369aceca9068862846199c6aa2 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Tue, 10 Nov 2026 04:42:36 +0800 Subject: [PATCH] Delete superseded refine reminders in the PR sweep A PR with open threads kept every earlier "not settled yet" reminder, because only the review that posts a new one trimmed the old copies and the sweep deleted them only once every thread was resolved. #561 showed four. The sweep now lists every copy but the newest as superseded, and apply-pr-actions.sh re-reads the live comments for each row before deleting one, so a concurrent review cannot leave the PR with none. The trim covers only PRs within 100 comments and 100 threads. Change-Id: Ib9c31f785a541290fe5094aeea21b21da736ea96 --- .../codetrial-triage/apply-pr-actions.sh | 58 ++++++- .claude/skills/codetrial-triage/pr-sweep.sh | 16 +- .claude/skills/codetrial-triage/pulls.md | 12 +- .claude/skills/codetrial-triage/review-prs.md | 4 +- tests/stubs/skill-scripts/gh-apply.sh | 8 + tests/test_skill_scripts.py | 153 +++++++++++++++++- 6 files changed, 236 insertions(+), 15 deletions(-) diff --git a/.claude/skills/codetrial-triage/apply-pr-actions.sh b/.claude/skills/codetrial-triage/apply-pr-actions.sh index a1b020a46..5e09ea690 100755 --- a/.claude/skills/codetrial-triage/apply-pr-actions.sh +++ b/.claude/skills/codetrial-triage/apply-pr-actions.sh @@ -225,7 +225,9 @@ done > "$work/hide.out" # A review whose every thread is resolved or outdated is settled, so it is # hidden as resolved; the thread stays where it is and the review is one click # from readable. A refine request left on a PR with no open thread is stale, so -# it is deleted: a reminder that no longer asks for anything only misleads. +# it is deleted: a reminder that no longer asks for anything only misleads. On a +# PR that still has open threads every copy but the newest is deleted too, so +# one current reminder stands. live_threads() { # shellcheck disable=SC2016 # GraphQL variables @@ -255,9 +257,59 @@ jq -r '.hide_resolved[] | [.pr, .id] | @tsv' "$sweep" | while read -r pr id; do receipt hide_resolved "$pr" failed "$id" fi done > "$work/hide-resolved.out" + +# The viewer's refine reminders on a PR, oldest first, each with its node id. +# Read for every row, so a copy review-finish.sh trimmed in between is seen. +live_reminders() +{ + # shellcheck disable=SC2016 # GraphQL variables + gh api graphql -F owner="${repo%/*}" -F name="${repo#*/}" -F pr="$1" -f query=' + query($owner: String!, $name: String!, $pr: Int!) { + repository(owner: $owner, name: $name) { pullRequest(number: $pr) { + comments(last: 100) { totalCount nodes { id createdAt body author { login } } } + } } + }' | jq --arg me "$(jq -r .viewer "$sweep")" --arg m "$(cat "$here/refine.md")" ' + .data.repository.pullRequest.comments.nodes |= ( + map(select(.author.login == $me and (.body | startswith($m)))) + | sort_by([.createdAt, .id]))' +} + +# Delete a reminder the sweep found superseded, whatever the threads say, but +# only while the live PR still has it as the viewer's reminder with a newer one +# standing: review-finish.sh may have trimmed the PR since the sweep. +delete_superseded() +{ + mine=$(live_reminders "$1") || { + receipt refine_stale "$1" failed "could not read comments" + return + } + place=$(printf '%s\n' "$mine" | jq -r --arg id "$2" ' + [.data.repository.pullRequest.comments.nodes[].id] as $ids + | ($ids | index($id)) as $at + | if $at == null and .data.repository.pullRequest.comments.totalCount > 100 then "unverified" + elif $at == null then "gone" + elif $at == ($ids | length) - 1 then "newest" + else "ok" end') || place= + case $place in + gone) receipt refine_stale "$1" already "$2 is gone" ;; + unverified) receipt refine_stale "$1" skipped "$2 is outside the last 100 comments" ;; + newest) receipt refine_stale "$1" skipped "$2 is now the only current reminder" ;; + ok) + if delete_comment "$2"; then + receipt refine_stale "$1" applied "deleted $2" + else + receipt refine_stale "$1" failed "$2" + fi + ;; + *) receipt refine_stale "$1" failed "could not verify $2" ;; + esac +} + # shellcheck disable=SC2016 # $id below is a GraphQL variable -jq -r '.refine_stale[] | [.pr, .id] | @tsv' "$sweep" | while read -r pr id; do - if ! state=$(live_threads "$pr"); then +jq -r '.refine_stale[] | [.pr, .id, (.why // "settled")] | @tsv' "$sweep" | while read -r pr id why; do + if [ "$why" = superseded ]; then + delete_superseded "$pr" "$id" + elif ! state=$(live_threads "$pr"); then receipt refine_stale "$pr" failed "could not read threads" elif ! printf '%s\n' "$state" | jq -e ' .data.repository.pullRequest.reviewThreads as $t diff --git a/.claude/skills/codetrial-triage/pr-sweep.sh b/.claude/skills/codetrial-triage/pr-sweep.sh index 568b22073..a0471879b 100755 --- a/.claude/skills/codetrial-triage/pr-sweep.sh +++ b/.claude/skills/codetrial-triage/pr-sweep.sh @@ -36,7 +36,9 @@ # thread that is resolved or outdated. Skipped when a thread is # too long to read whole # refine_stale the viewer's "Some review comments ... not settled yet" -# comments (refine.md) on a PR with no open thread left, to delete +# comments (refine.md) to delete: all of them on a PR with no open +# thread left (why settled), and every one but the newest on a PR +# that still has open threads (why superseded) # unknown PRs whose mergeability GitHub has still not computed after one # retry; rerun # status every open PR, smallest first: lines changed split into source @@ -591,11 +593,13 @@ def refs: (ref_prefix + "[0-9]+\\b") as $one and (.body == "" or .author.__typename == "Bot")) | {pr: $n, id, by: .author.login}], refine_stale: [$prs[] | .number as $n - | select(whole and .reviewThreads.totalCount <= 100 - and all(.reviewThreads.nodes[]; .isResolved)) - | .comments.nodes[] - | select(.author.login == $me and (.body | startswith($refine))) - | {pr: $n, id}], + | select(whole and .reviewThreads.totalCount <= 100) + | ([.comments.nodes[] + | select(.author.login == $me and (.body | startswith($refine)))] + | sort_by([.createdAt, .id])) as $mine + | (all(.reviewThreads.nodes[]; .isResolved)) as $settled + | (if $settled then $mine else $mine[:-1] end)[] + | {pr: $n, id, why: (if $settled then "settled" else "superseded" end)}], maintainers_known: ($pushers != null), viewer: $me, prerequisites: prerequisites, diff --git a/.claude/skills/codetrial-triage/pulls.md b/.claude/skills/codetrial-triage/pulls.md index ee178ee64..e5106f708 100644 --- a/.claude/skills/codetrial-triage/pulls.md +++ b/.claude/skills/codetrial-triage/pulls.md @@ -122,10 +122,14 @@ list instead. sits in a thread that is resolved or outdated, as #633's cubic review was; the thread, the review state and the grade are untouched, and the review is one click from readable. The second is the viewer's own [refine.md](refine.md) - reminder on a PR with no open thread left; it is deleted, not hidden. A - reminder on a PR that still has open threads is replaced by the review's next - one ([review-prs.md](review-prs.md#check-every-open-thread)). Both are - re-checked against live threads right before the write. + reminder; it is deleted, not hidden. On a PR with no open thread left every + copy goes (`why: settled`). On a PR that still has open threads every copy + but the newest goes (`why: superseded`), so one current reminder stands even + when an earlier run left several; the review's next reminder replaces that + one ([review-prs.md](review-prs.md#check-every-open-thread)). A `settled` + entry is re-checked against live threads right before the write, and a + `superseded` one against the live comments (it is kept if it has become the + newest). - On a bare `/codetrial-triage` only, requesting changes on each open PR whose `hygiene.sh` output has items, as [Contributor hygiene](#contributor-hygiene) describes, and posting each diff --git a/.claude/skills/codetrial-triage/review-prs.md b/.claude/skills/codetrial-triage/review-prs.md index 2718c3da2..4d2d80f8f 100644 --- a/.claude/skills/codetrial-triage/review-prs.md +++ b/.claude/skills/codetrial-triage/review-prs.md @@ -328,7 +328,9 @@ author (a thread holding only the maintainer's own comments is not one), not only the new ones, and the comment replaces the viewer's earlier reminders on the PR: once it is posted they are deleted, so a PR carries one current reminder and no stale copies. A reminder that fails to post deletes nothing. When -every thread is resolved the sweep deletes the leftover reminder itself +every thread is resolved the sweep deletes the leftover reminder itself, and +while threads are open it trims all but the newest copy, on PRs within 100 +comments and 100 threads (`refine_stale`, [pulls.md](pulls.md#what-a-sweep-may-apply-without-asking)). The PR stays graded `low` while threads are open, and the `stale` nudge in [pulls.md](pulls.md) reminds an author who goes quiet. A thread opened without a diff --git a/tests/stubs/skill-scripts/gh-apply.sh b/tests/stubs/skill-scripts/gh-apply.sh index 6de72ae67..00b481cce 100644 --- a/tests/stubs/skill-scripts/gh-apply.sh +++ b/tests/stubs/skill-scripts/gh-apply.sh @@ -38,6 +38,14 @@ case "$*" in fi : > "$FAKE_LOG.written" ;; + "api graphql "*"comments(last"*) + # The PR's comments as GraphQL returns them; the test builds the body. + if [ -n "${FAKE_COMMENTS:-}" ]; then + printf '%s\n' "$FAKE_COMMENTS" + else + echo '{"data":{"repository":{"pullRequest":{"comments":{"totalCount":0,"nodes":[]}}}}}' + fi + ;; "api graphql "*"query("*) if [ -n "${FAKE_THREADS_AFTER_WRITE:-}" ] && [ -f "$FAKE_LOG.written" ]; then printf '%s\n' "$FAKE_THREADS_AFTER_WRITE" diff --git a/tests/test_skill_scripts.py b/tests/test_skill_scripts.py index dd31ee1dc..09dd226d8 100644 --- a/tests/test_skill_scripts.py +++ b/tests/test_skill_scripts.py @@ -64,6 +64,14 @@ def ago(days): return moment.strftime("%Y-%m-%dT%H:%M:%SZ") +REFINE_TEXT = (SKILLS / "codetrial-triage" / "refine.md").read_text() + + +def reminder(id, days=1): + """A refine.md reminder from the viewer, as the sweep reads it.""" + return comment("viewer", days, REFINE_TEXT) | {"id": id} + + def comment(login, days, body="Please post the `--version` line."): return { "author": {"__typename": "User", "login": login}, @@ -674,7 +682,60 @@ def test_a_refine_comment_is_stale_once_no_thread_is_open(self): pull(1, comments=comments, reviewThreads=settled), pull(2, comments=comments, reviewThreads=listed(thread())), ) - self.assertEqual(swept["refine_stale"], [{"pr": 1, "id": "IC_mine"}]) + self.assertEqual( + swept["refine_stale"], [{"pr": 1, "id": "IC_mine", "why": "settled"}] + ) + + def test_only_the_newest_refine_comment_stands_while_a_thread_is_open(self): + first, second, third = reminder("IC_1"), reminder("IC_2"), reminder("IC_3") + swept = self.run_pulls( + pull( + 1, + comments=listed(first, second, third), + reviewThreads=listed(thread()), + ) + ) + self.assertEqual( + swept["refine_stale"], + [ + {"pr": 1, "id": "IC_1", "why": "superseded"}, + {"pr": 1, "id": "IC_2", "why": "superseded"}, + ], + ) + + def test_the_newest_refine_comment_is_chosen_by_time_not_by_list_position(self): + newest, oldest = reminder("IC_new", 1), reminder("IC_old", 5) + swept = self.run_pulls( + pull( + 1, + comments=listed(newest, oldest), + reviewThreads=listed(thread()), + ) + ) + self.assertEqual( + swept["refine_stale"], [{"pr": 1, "id": "IC_old", "why": "superseded"}] + ) + + def test_one_refine_comment_stands_while_a_thread_is_open(self): + only = reminder("IC_1") + swept = self.run_pulls( + pull(1, comments=listed(only), reviewThreads=listed(thread())) + ) + self.assertEqual(swept["refine_stale"], []) + + def test_every_refine_comment_goes_once_all_threads_are_resolved(self): + one, two = reminder("IC_1"), reminder("IC_2") + swept = self.run_pulls( + pull( + 1, + comments=listed(one, two), + reviewThreads=listed(thread() | {"isResolved": True}), + ) + ) + self.assertEqual( + [(r["id"], r["why"]) for r in swept["refine_stale"]], + [("IC_1", "settled"), ("IC_2", "settled")], + ) def test_a_thread_the_author_opened_is_not_a_reviewer_thread(self): grades = self.grades( @@ -3826,6 +3887,7 @@ class ApplyPrActionsTests(unittest.TestCase): def sweep(self, **fields): base = { "maintainers_known": True, + "viewer": "viewer", "prs": [{"number": n, "head": f"h{n}"} for n in (1, 2, 3, 4)], "approve": [ self.approval()["approve"][0], @@ -4012,6 +4074,95 @@ def test_resolved_reviews_are_hidden_and_a_stale_refine_comment_deleted(self): self.assertIn("classifier: RESOLVED", log) self.assertIn("deleteIssueComment", log) + def reminders(self, *nodes, total=None): + text = REFINE_TEXT + return json.dumps( + { + "data": { + "repository": { + "pullRequest": { + "comments": { + "totalCount": total or len(nodes), + "nodes": [ + { + "id": n, + "createdAt": f"2026-11-0{day}T00:00:00Z", + "author": {"login": by}, + "body": text if by == "viewer" else "hi", + } + for n, by, day in nodes + ], + } + } + } + } + } + ) + + def superseded(self, comments, **env): + return self.run_actions( + self.sweep( + **self.QUIET, + refine_stale=[{"pr": 2, "id": "IC_old", "why": "superseded"}], + ), + FAKE_COMMENTS=comments, + **env, + ) + + def test_a_superseded_refine_comment_is_deleted_without_reading_threads(self): + _, got, log = self.superseded( + self.reminders(("IC_old", "viewer", 1), ("IC_new", "viewer", 2)) + ) + self.assertEqual(got, {("refine_stale", 2): "applied"}) + self.assertIn("deleteIssueComment", log) + self.assertNotIn("isResolved", log) + + def test_a_superseded_refine_comment_that_became_the_newest_is_kept(self): + _, got, log = self.superseded(self.reminders(("IC_old", "viewer", 1))) + self.assertEqual(got, {("refine_stale", 2): "skipped"}) + self.assertNotIn("deleteIssueComment", log) + + def test_a_superseded_refine_comment_already_gone_is_not_a_failure(self): + _, got, log = self.superseded(self.reminders(("IC_new", "viewer", 1))) + self.assertEqual(got, {("refine_stale", 2): "already"}) + self.assertNotIn("deleteIssueComment", log) + + def test_another_authors_comment_never_counts_as_the_newer_reminder(self): + _, got, log = self.superseded( + self.reminders(("IC_old", "viewer", 1), ("IC_other", "someone", 1)) + ) + self.assertEqual(got, {("refine_stale", 2): "skipped"}) + self.assertNotIn("deleteIssueComment", log) + + def test_the_newest_reminder_is_chosen_by_time_not_by_list_position(self): + # IC_old is listed last but is the older: it is deleted, not kept. + _, got, log = self.superseded( + self.reminders(("IC_new", "viewer", 2), ("IC_old", "viewer", 1)) + ) + self.assertEqual(got, {("refine_stale", 2): "applied"}) + self.assertIn("deleteIssueComment", log) + + def test_a_comment_past_the_last_hundred_is_not_reported_gone(self): + _, got, log = self.superseded( + self.reminders(("IC_new", "viewer", 1), total=101) + ) + self.assertEqual(got, {("refine_stale", 2): "skipped"}) + self.assertNotIn("deleteIssueComment", log) + + def test_a_refine_entry_without_a_reason_is_checked_against_the_threads(self): + _, got, log = self.run_actions( + self.sweep(**self.QUIET, refine_stale=[{"pr": 2, "id": "IC_9"}]) + ) + self.assertEqual(got, {("refine_stale", 2): "applied"}) + self.assertIn("isResolved", log) + + def test_a_failed_superseded_delete_is_reported(self): + _, got, _ = self.superseded( + self.reminders(("IC_old", "viewer", 1), ("IC_new", "viewer", 2)), + FAKE_REFUSED="1", + ) + self.assertEqual(got, {("refine_stale", 2): "failed"}) + def test_a_write_that_timed_out_is_tried_again_before_it_is_reported(self): quiet = self.QUIET _, got, log = self.run_actions(