From ad9e391da5ce5613b2680bb29a400299e360618f Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 22 Nov 2026 18:50:12 -0600 Subject: [PATCH] fix(queue): make merge-watch coverage imports valid --- scripts/fleet/queue-reconcile/README.md | 20 +++--- .../fleet/queue-reconcile/check_coverage.py | 63 ++++++++++--------- .../queue-reconcile/test_check_coverage.py | 44 +++++++++++-- 3 files changed, 84 insertions(+), 43 deletions(-) diff --git a/scripts/fleet/queue-reconcile/README.md b/scripts/fleet/queue-reconcile/README.md index abf25dbb59..8eca043f8e 100644 --- a/scripts/fleet/queue-reconcile/README.md +++ b/scripts/fleet/queue-reconcile/README.md @@ -40,9 +40,10 @@ declaring an issue uncovered (branch `queue/--*`, a closing keyword, or a trailing `(#n)` title reference). An issue with an open fix PR is in-flight: no triage packet. With `--import` it gets one merge-watch packet per PR (dedupe -`repo-health:v1:merge-watch::pr:`) — the ~24h recheck that -re-tests mergibility, refreshes the branch, merges, or closes the PR -with a salvage note, instead of letting pr_opened work bit-rot. +`repo-health:v1:pr::pr:`) — the ~24h recheck that re-tests +mergibility, refreshes the branch, merges, or closes the PR with a +salvage note, instead of letting pr_opened work bit-rot. If several +open PRs cover one issue, each PR gets its own watch task. **Ready-PR shepherd fallback (#12280):** the stale-PR warden is the merge sweep, but it is one scheduled job — when its live job vanished @@ -50,13 +51,12 @@ in the November 2026 environment recovery, ready PRs sat unmerged for days while the schedule registry still listed the job enabled. The reconciliation is the fallback that does not depend on it: the `/coverage` call also carries the repo's open PR numbers, and the -broker answers which of them a **live** task names (either PR key -class — the warden's `repo-health:v1:pr::pr:` or the -merge-watch key above). With `--import`, any ready (non-draft) PR -older than 6 hours with no live task naming it gets one `pr_shepherd` -packet under the warden's own dedupe key, so the warden and this -fallback dedupe onto a single live task whichever files first. The -packet's done rule: merged, or closed with a reason recorded. A PR +broker answers which of them a **live** task names (the canonical +`repo-health:v1:pr::pr:` key shared by the warden and +merge-watch). With `--import`, any ready (non-draft) PR older than 6 +hours with no live task naming it gets one `pr_shepherd` packet under +that same key, so the warden and this fallback share one live task per PR. +The packet's done rule: merged, or closed with a reason recorded. A PR already receiving a merge-watch packet in the same run is not double-filed. diff --git a/scripts/fleet/queue-reconcile/check_coverage.py b/scripts/fleet/queue-reconcile/check_coverage.py index d07608fecb..e7c1f8a10d 100644 --- a/scripts/fleet/queue-reconcile/check_coverage.py +++ b/scripts/fleet/queue-reconcile/check_coverage.py @@ -55,13 +55,12 @@ recovery and ready PRs sat unmerged for days. So this reconciliation also diffs the repo's ready (non-draft) PRs against live queue tasks: the broker `/coverage` call carries the PR numbers and answers which -of them a LIVE task names (the warden's `pr` dedupe key or the -merge-watch key — done/cancelled tasks are not coverage, same rule -as issues). In `--import` mode, any ready PR older than 6 hours with -no live task naming it gets one `pr_shepherd` packet under the -warden's own dedupe key, so the warden and this fallback dedupe onto -a single live task whichever files first. A PR already receiving a -merge-watch packet this run is not double-filed. Fixtures carry the +of them a LIVE task names. Merge-watch and ready-PR shepherd packets +use the same canonical key, `repo-health:v1:pr::pr:`, so the +warden and reconciliation dedupe onto one live task whichever files +first. In `--import` mode, any ready PR older than 6 hours with no +live task naming it gets one `pr_shepherd` packet under that key. A PR +already receiving a merge-watch packet this run is not double-filed. Fixtures carry the broker's PR answer as `"uncovered_pulls": [{"number": n}]` inside each repo's coverage object; a fixture without it treats every PR as uncovered (the pre-#12280 fixture semantics). @@ -210,9 +209,9 @@ def pr_issue_numbers(pr: dict) -> set[int]: return numbers -def build_pr_coverage(prs: list[dict]) -> dict[int, dict]: - """issue number -> the open PR covering it (first PR wins).""" - coverage: dict[int, dict] = {} +def build_pr_coverage(prs: list[dict]) -> dict[int, list[dict]]: + """Issue number -> all open PRs covering it, in PR enumeration order.""" + coverage: dict[int, list[dict]] = {} for pr in prs: info = { "number": pr["number"], @@ -220,7 +219,7 @@ def build_pr_coverage(prs: list[dict]) -> dict[int, dict]: "branch": ((pr.get("head") or {}).get("ref") or ""), } for n in pr_issue_numbers(pr): - coverage.setdefault(n, info) + coverage.setdefault(n, []).append(info) return coverage @@ -352,12 +351,17 @@ def merge_watch_packet(repo: str, issue: dict, pr: dict, checked_at: str) -> dic "note if main has superseded it." ), "priority": 30, - "dedupe_key": f"repo-health:v1:merge-watch:{repo}:pr:{pr_number}", + # Share the warden's canonical key so all shepherd paths dedupe + # onto one live task per PR and the broker's coverage matcher sees it. + "dedupe_key": f"repo-health:v1:pr:{repo}:pr:{pr_number}", "payload": { "schema": "repo-task/v1", "kind": "pr_shepherd", "repo": repo, "queue": None, # filled by the caller with the pilot queue + "prs": [ + {"number": pr_number, "url": pr_url, "title": pr.get("title") or ""} + ], "issue": { "number": number, "url": issue_url, @@ -406,7 +410,7 @@ def merge_watch_packet(repo: str, issue: dict, pr: dict, checked_at: str) -> dic "coverage reconciliation (#4951)." ), "timebox_minutes": 45, - "max_attempts": 4, + "max_attempts": 1000, "context_refs": [issue_url, pr_url], "dedupe": { "checked_at": checked_at, @@ -654,11 +658,11 @@ def render_report( "", ] for r, i in pr_any: - pr = i.get("pr") or {} - lines.append( - f"- {r['repo']}#{i['number']} — PR #{pr.get('number')} " - f"({pr.get('branch') or 'branch unknown'})" - ) + for pr in i.get("prs", []): + lines.append( + f"- {r['repo']}#{i['number']} — PR #{pr.get('number')} " + f"({pr.get('branch') or 'branch unknown'})" + ) lines.append("") uncovered_any = [(r, i) for r in results for i in r.get("uncovered_issues", [])] if uncovered_any: @@ -760,7 +764,7 @@ def main(argv: list[str] | None = None) -> int: broker_uncovered = cov.get("uncovered_issues", []) uncovered = [i for i in broker_uncovered if i["number"] not in pr_coverage] pr_covered = [ - {**i, "pr": pr_coverage[i["number"]]} + {**i, "prs": pr_coverage[i["number"]]} for i in broker_uncovered if i["number"] in pr_coverage ] @@ -781,16 +785,17 @@ def main(argv: list[str] | None = None) -> int: watch_imported = 0 if do_import and pr_covered: for issue in pr_covered: - packet = merge_watch_packet(repo, issue, issue["pr"], checked_at) - ok, detail = enqueue_packet( - packet, queue, args.fleet, args.broker_url, args.token_file - ) - if ok: - watch_imported += 1 - else: - errors.append( - f"merge-watch enqueue failed for {repo}#{issue['number']}: {detail}" + for pr in issue["prs"]: + packet = merge_watch_packet(repo, issue, pr, checked_at) + ok, detail = enqueue_packet( + packet, queue, args.fleet, args.broker_url, args.token_file ) + if ok: + watch_imported += 1 + else: + errors.append( + f"merge-watch enqueue failed for {repo}#{pr['number']}: {detail}" + ) # #12280: ready PRs older than 6h with no live task naming # them get a warden-keyed shepherd packet. The broker's PR @@ -820,7 +825,7 @@ def main(argv: list[str] | None = None) -> int: ready_shepherd_candidates( pulls, uncovered_pull_numbers, - {i["pr"]["number"] for i in pr_covered}, + {pr["number"] for i in pr_covered for pr in i["prs"]}, checked_at, ) if pr_diff_known diff --git a/scripts/fleet/queue-reconcile/test_check_coverage.py b/scripts/fleet/queue-reconcile/test_check_coverage.py index 2f8c676379..4ed5bdb9d9 100644 --- a/scripts/fleet/queue-reconcile/test_check_coverage.py +++ b/scripts/fleet/queue-reconcile/test_check_coverage.py @@ -303,7 +303,42 @@ def test_open_pr_issue_gets_one_merge_watch_packet(self): packet = calls[0]["packet"] self.assertEqual(packet["payload"]["kind"], "pr_shepherd") self.assertEqual(packet["payload"]["pr"]["number"], 900) - self.assertTrue(packet["dedupe_key"].endswith(":pr:900")) + self.assertEqual(packet["payload"]["prs"][0]["number"], 900) + self.assertEqual( + packet["dedupe_key"], f"repo-health:v1:pr:{REPO}:pr:900" + ) + self.assertEqual(packet["payload"]["max_attempts"], 1000) + + def test_each_open_pr_for_one_issue_gets_its_own_merge_watch(self): + pulls = [ + { + "number": 900, + "title": "first slice (#3)", + "body": "", + "head": {"ref": "queue/111-3-first"}, + }, + { + "number": 901, + "title": "second slice (#3)", + "body": "", + "head": {"ref": "queue/112-3-second"}, + }, + ] + code, _ = self.run_cli(fixture([], [3], pulls), "--import") + self.assertEqual(code, 0) + calls = self.fleet.calls() + self.assertEqual(len(calls), 2) + self.assertEqual( + {call["packet"]["payload"]["pr"]["number"] for call in calls}, + {900, 901}, + ) + self.assertEqual( + {call["packet"]["dedupe_key"] for call in calls}, + { + f"repo-health:v1:pr:{REPO}:pr:900", + f"repo-health:v1:pr:{REPO}:pr:901", + }, + ) def test_observe_mode_reports_pr_coverage_and_enqueues_nothing(self): pulls = [ @@ -481,10 +516,11 @@ def test_merge_watch_pr_is_not_double_filed_as_shepherd(self): code, _ = self.run_cli(fx, "--import") self.assertEqual(code, 0) calls = self.fleet.calls() - # Exactly one packet: the merge-watch, keyed merge-watch. + # Exactly one packet: the merge-watch, keyed by the canonical PR key. self.assertEqual(len(calls), 1) - self.assertTrue( - calls[0]["packet"]["dedupe_key"].startswith("repo-health:v1:merge-watch:") + self.assertEqual( + calls[0]["packet"]["dedupe_key"], + f"repo-health:v1:pr:{REPO}:pr:954", ) def test_observe_mode_reports_candidates_and_enqueues_nothing(self):