Repository navigation
[trymerge] Add the greenlight land-time guard and its tests - #194840
jeanschmidt wants to merge 3 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194840
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 19 PendingAs of commit aa8b8b6 with merge base e812f6f ( GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:
reason: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
| else: | ||
| outcomes = [_evaluate_pr(pr, states.get(pr.pr_num), moment) for pr in guarded] | ||
|
|
||
| unresolved = [o for o in outcomes if o.verdict is not GuardVerdict.ALLOW] |
There was a problem hiding this comment.
Nit: double negative is a bit hard to read, why not just say if all verdict are GuardVerdict.ALLOW, then GuardVerdict.ALLOW
There was a problem hiding this comment.
claude came up with an even better code, let me submit it :)
| return GuardResult(GuardVerdict.ALLOW) | ||
|
|
||
| try: | ||
| states = fetch(repo_full_name, [pr.pr_num for pr in guarded]) |
There was a problem hiding this comment.
How does this fetch handle missing or invalid HUD_API_TOKEN? My read here is that when it happens, for example, we rotate the token, greenlight guard would continue to wait. I'm trying to debate with myself if this is a good or bad behaviour. I'm leaning more on the latter because HUD_API_TOKEN has already been set in the ongoing merge job and can't be updated.
Wdyt about?
- Receiving client side 400/401/405 -> fail fast, and surface the error
- Receiving flaky 5xx -> retry again up to MAX_WAIT_MINUTES
There was a problem hiding this comment.
you are right, in this case failing fast is better, given that there is no possible recover except from cancel the job and try again. Albeit this would be a very exotic occurrence...
`greenlight_guard.py` is the decision itself. It compares the head SHA greenlight recorded in its ledger against the head actually being landed and returns ALLOW, WAIT or DENY, along with the wait budget (`GreenlightWaitWindow`) a merge carries across re-entries. Everything it needs about a PR arrives as plain data or as a callable on `PRUnderMerge` — the callables stay callables because each one costs a GitHub request that only the minority of PRs greenlight alone authorizes ever needs. It must not import `trymerge`: `trymerge` imports it, and the cycle would break both. Two failure directions are split deliberately. Anything that is only unknown — no ledger row, a verdict for an older commit, a review still running, an unreadable HUD, a reviewer GitHub would not classify — waits, and is refused only once the budget is spent. Anything settled against the head being landed — NO_LAND/CANCELLED/FAILED, an unrecognized status, a review that has outlived greenlight's own timeout — refuses at once. Refusing on first uncertainty would turn a HUD blip into a dead merge; waiting on a settled negative would burn an hour and land nothing. The tests cover the decision table, stack handling, the laziness of the per-PR GitHub lookups, the ledger's transport failure modes against a mocked `gh_fetch_url` (every documented route error code, malformed payloads, batch capping, backoff, recovery on retry), and the preflight mirror. No test touches the network. They land together with the guard because `test_greenlight_guard.py` owns the shared PR fixtures that the ledger and preflight tests build on. Split out from the `trymerge.py` wiring so the verdict logic can be reviewed against greenlight's behaviour independently of the merge path, and to keep each change under the 2000 LOC infra limit. Nothing calls into this yet. Signed-off-by: Jean Schmidt <[email protected]> ghstack-source-id: 3865cf8 Pull-Request: #194840
|
Implemented the changes, from claude: You were right about the token: a rotated secret meant an hour of retries that I didn't take the status-code split, though. Most codes we'd actually see don't Instead: a missing or malformed token is detectable locally, so that refuses Nit: done. I also fixed two ways the guard could have allowed a merge it shouldn't have, Left alone, flagging rather than burying: the same secret is read elsewhere in |
Superseded by a newer greenlight verdict.
|
Starting merge as part of PR stack under #194841 |
…194841) **Impact:** mergebot only — PRs whose sole merge authorization is the `pytorchgreenlight` approval **Risk:** medium Depends on pytorch/test-infra#8630 to be merged before merging this one ## What Wires the greenlight land-time guard into `merge_into`: when the only approval that satisfies a merge rule is greenlight's, trymerge reads greenlight's verdict ledger and refuses to land unless greenlight recorded `LAND` for the head commit that is actually being merged. The verdict logic already lives in the `greenlight_*.py` modules next to trymerge; this change only feeds the PR facts in and turns the verdict into the exception the merge loop already understands. Three smaller changes in `trymerge.py` that are worth reading on their own: - `find_matching_merge_rule` gains an `approved_by_override` kwarg, so the guard can ask "would any rule still match if greenlight had not approved?" without duplicating rule evaluation. Default behaviour is unchanged. - The inline approver expansion is extracted to `rule_approvers`, and a malformed team ref (`org/team/extra`) now raises instead of expanding to nobody. Previously `gh_get_team_members` warned, returned `[]`, and the rule then matched *any* approver. - Two REST accessors (`get_updated_at`, `get_bot_reviewers`) added on `GitHubPR`. They are REST rather than fields on `GH_GET_PR_INFO_QUERY` because that query text is the cache key for the recorded GraphQL fixtures — extending it invalidates all of them. ## Why `merge_rules.yaml`'s "Greenlight Review Bot" rule lets a single `pytorchgreenlight` approval authorize a merge of any path, and pytorch/pytorch has no GitHub-native required reviews, so that approval is never dismissed when the author pushes more commits. Today an author can get greenlight's approval on a benign commit, push anything on top, and `@pytorchbot merge` lands unreviewed content. This closes that window by comparing the head SHA greenlight actually evaluated against the head being landed. # Notes Behaviour reviewers should sanity-check: - **Scope.** The guard is skipped entirely when greenlight did not approve, and when any other merge rule still matches after dropping greenlight's approval. A drive-by approval from a random GitHub user does *not* switch it off. - **Waiting.** "greenlight hasn't answered yet" raises `MandatoryChecksMissingError`, so the existing 5-minute retry loop handles it. The 60-minute budget is owned by `merge()` and shared across re-entries, and opens at the first wait (not at command time, since `merge_into` is only reached once CI is green). One comment is posted to the PR the first time it waits. - **Force merge.** `-f` gets no wait window and therefore never waits; it refuses instead if greenlight is the only authority and hasn't verified the head. - **ghstack.** Checked against each open PR's GitHub head, which is what greenlight records, not the `/orig` revision git cherry-picks. - **Failure modes.** A HUD outage is a wait, not a refusal, until the budget runs out; an unreadable `merge_rules.yaml` or an unclassifiable reviewer leaves the guard on rather than inventing an authorization. Depends on the `/api/greenlight/pr_state` route in pytorch/test-infra; `HUD_API_TOKEN` is already plumbed into `trymerge.yml`. This is the last change of the guard and the only one that alters existing behaviour; the modules it calls landed ahead of it so that the merge-path wiring could be reviewed on its own and each change stay under the 2000 LOC infra limit. One test here asserts `merge_rules.yaml` still grants `pytorchgreenlight` merge authority, so a rename there cannot silently disable the guard. Signed-off-by: Jean Schmidt <[email protected]> Pull Request resolved: #194841 Approved by: https://github.com/huydhn ghstack dependencies: #194838, #194839, #194840
…194840) `greenlight_guard.py` is the decision itself. It compares the head SHA greenlight recorded in its ledger against the head actually being landed and returns ALLOW, WAIT or DENY, along with the wait budget (`GreenlightWaitWindow`) a merge carries across re-entries. Everything it needs about a PR arrives as plain data or as a callable on `PRUnderMerge` — the callables stay callables because each one costs a GitHub request that only the minority of PRs greenlight alone authorizes ever needs. It must not import `trymerge`: `trymerge` imports it, and the cycle would break both. Two failure directions are split deliberately. Anything that is only unknown — no ledger row, a verdict for an older commit, a review still running, an unreadable HUD, a reviewer GitHub would not classify — waits, and is refused only once the budget is spent. Anything settled against the head being landed — NO_LAND/CANCELLED/FAILED, an unrecognized status, a review that has outlived greenlight's own timeout — refuses at once. Refusing on first uncertainty would turn a HUD blip into a dead merge; waiting on a settled negative would burn an hour and land nothing. The tests cover the decision table, stack handling, the laziness of the per-PR GitHub lookups, the ledger's transport failure modes against a mocked `gh_fetch_url` (every documented route error code, malformed payloads, batch capping, backoff, recovery on retry), and the preflight mirror. No test touches the network. They land together with the guard because `test_greenlight_guard.py` owns the shared PR fixtures that the ledger and preflight tests build on. Split out from the `trymerge.py` wiring so the verdict logic can be reviewed against greenlight's behaviour independently of the merge path, and to keep each change under the 2000 LOC infra limit. Nothing calls into this yet. Signed-off-by: Jean Schmidt <[email protected]> Pull Request resolved: pytorch#194840 Approved by: https://github.com/huydhn ghstack dependencies: pytorch#194838, pytorch#194839
…ytorch#194841) **Impact:** mergebot only — PRs whose sole merge authorization is the `pytorchgreenlight` approval **Risk:** medium Depends on pytorch/test-infra#8630 to be merged before merging this one ## What Wires the greenlight land-time guard into `merge_into`: when the only approval that satisfies a merge rule is greenlight's, trymerge reads greenlight's verdict ledger and refuses to land unless greenlight recorded `LAND` for the head commit that is actually being merged. The verdict logic already lives in the `greenlight_*.py` modules next to trymerge; this change only feeds the PR facts in and turns the verdict into the exception the merge loop already understands. Three smaller changes in `trymerge.py` that are worth reading on their own: - `find_matching_merge_rule` gains an `approved_by_override` kwarg, so the guard can ask "would any rule still match if greenlight had not approved?" without duplicating rule evaluation. Default behaviour is unchanged. - The inline approver expansion is extracted to `rule_approvers`, and a malformed team ref (`org/team/extra`) now raises instead of expanding to nobody. Previously `gh_get_team_members` warned, returned `[]`, and the rule then matched *any* approver. - Two REST accessors (`get_updated_at`, `get_bot_reviewers`) added on `GitHubPR`. They are REST rather than fields on `GH_GET_PR_INFO_QUERY` because that query text is the cache key for the recorded GraphQL fixtures — extending it invalidates all of them. ## Why `merge_rules.yaml`'s "Greenlight Review Bot" rule lets a single `pytorchgreenlight` approval authorize a merge of any path, and pytorch/pytorch has no GitHub-native required reviews, so that approval is never dismissed when the author pushes more commits. Today an author can get greenlight's approval on a benign commit, push anything on top, and `@pytorchbot merge` lands unreviewed content. This closes that window by comparing the head SHA greenlight actually evaluated against the head being landed. # Notes Behaviour reviewers should sanity-check: - **Scope.** The guard is skipped entirely when greenlight did not approve, and when any other merge rule still matches after dropping greenlight's approval. A drive-by approval from a random GitHub user does *not* switch it off. - **Waiting.** "greenlight hasn't answered yet" raises `MandatoryChecksMissingError`, so the existing 5-minute retry loop handles it. The 60-minute budget is owned by `merge()` and shared across re-entries, and opens at the first wait (not at command time, since `merge_into` is only reached once CI is green). One comment is posted to the PR the first time it waits. - **Force merge.** `-f` gets no wait window and therefore never waits; it refuses instead if greenlight is the only authority and hasn't verified the head. - **ghstack.** Checked against each open PR's GitHub head, which is what greenlight records, not the `/orig` revision git cherry-picks. - **Failure modes.** A HUD outage is a wait, not a refusal, until the budget runs out; an unreadable `merge_rules.yaml` or an unclassifiable reviewer leaves the guard on rather than inventing an authorization. Depends on the `/api/greenlight/pr_state` route in pytorch/test-infra; `HUD_API_TOKEN` is already plumbed into `trymerge.yml`. This is the last change of the guard and the only one that alters existing behaviour; the modules it calls landed ahead of it so that the merge-path wiring could be reviewed on its own and each change stay under the 2000 LOC infra limit. One test here asserts `merge_rules.yaml` still grants `pytorchgreenlight` merge authority, so a rename there cannot silently disable the guard. Signed-off-by: Jean Schmidt <[email protected]> Pull Request resolved: pytorch#194841 Approved by: https://github.com/huydhn ghstack dependencies: pytorch#194838, pytorch#194839, pytorch#194840
…194840) `greenlight_guard.py` is the decision itself. It compares the head SHA greenlight recorded in its ledger against the head actually being landed and returns ALLOW, WAIT or DENY, along with the wait budget (`GreenlightWaitWindow`) a merge carries across re-entries. Everything it needs about a PR arrives as plain data or as a callable on `PRUnderMerge` — the callables stay callables because each one costs a GitHub request that only the minority of PRs greenlight alone authorizes ever needs. It must not import `trymerge`: `trymerge` imports it, and the cycle would break both. Two failure directions are split deliberately. Anything that is only unknown — no ledger row, a verdict for an older commit, a review still running, an unreadable HUD, a reviewer GitHub would not classify — waits, and is refused only once the budget is spent. Anything settled against the head being landed — NO_LAND/CANCELLED/FAILED, an unrecognized status, a review that has outlived greenlight's own timeout — refuses at once. Refusing on first uncertainty would turn a HUD blip into a dead merge; waiting on a settled negative would burn an hour and land nothing. The tests cover the decision table, stack handling, the laziness of the per-PR GitHub lookups, the ledger's transport failure modes against a mocked `gh_fetch_url` (every documented route error code, malformed payloads, batch capping, backoff, recovery on retry), and the preflight mirror. No test touches the network. They land together with the guard because `test_greenlight_guard.py` owns the shared PR fixtures that the ledger and preflight tests build on. Split out from the `trymerge.py` wiring so the verdict logic can be reviewed against greenlight's behaviour independently of the merge path, and to keep each change under the 2000 LOC infra limit. Nothing calls into this yet. Signed-off-by: Jean Schmidt <[email protected]> Pull Request resolved: pytorch#194840 Approved by: https://github.com/huydhn ghstack dependencies: pytorch#194838, pytorch#194839
…ytorch#194841) **Impact:** mergebot only — PRs whose sole merge authorization is the `pytorchgreenlight` approval **Risk:** medium Depends on pytorch/test-infra#8630 to be merged before merging this one ## What Wires the greenlight land-time guard into `merge_into`: when the only approval that satisfies a merge rule is greenlight's, trymerge reads greenlight's verdict ledger and refuses to land unless greenlight recorded `LAND` for the head commit that is actually being merged. The verdict logic already lives in the `greenlight_*.py` modules next to trymerge; this change only feeds the PR facts in and turns the verdict into the exception the merge loop already understands. Three smaller changes in `trymerge.py` that are worth reading on their own: - `find_matching_merge_rule` gains an `approved_by_override` kwarg, so the guard can ask "would any rule still match if greenlight had not approved?" without duplicating rule evaluation. Default behaviour is unchanged. - The inline approver expansion is extracted to `rule_approvers`, and a malformed team ref (`org/team/extra`) now raises instead of expanding to nobody. Previously `gh_get_team_members` warned, returned `[]`, and the rule then matched *any* approver. - Two REST accessors (`get_updated_at`, `get_bot_reviewers`) added on `GitHubPR`. They are REST rather than fields on `GH_GET_PR_INFO_QUERY` because that query text is the cache key for the recorded GraphQL fixtures — extending it invalidates all of them. ## Why `merge_rules.yaml`'s "Greenlight Review Bot" rule lets a single `pytorchgreenlight` approval authorize a merge of any path, and pytorch/pytorch has no GitHub-native required reviews, so that approval is never dismissed when the author pushes more commits. Today an author can get greenlight's approval on a benign commit, push anything on top, and `@pytorchbot merge` lands unreviewed content. This closes that window by comparing the head SHA greenlight actually evaluated against the head being landed. # Notes Behaviour reviewers should sanity-check: - **Scope.** The guard is skipped entirely when greenlight did not approve, and when any other merge rule still matches after dropping greenlight's approval. A drive-by approval from a random GitHub user does *not* switch it off. - **Waiting.** "greenlight hasn't answered yet" raises `MandatoryChecksMissingError`, so the existing 5-minute retry loop handles it. The 60-minute budget is owned by `merge()` and shared across re-entries, and opens at the first wait (not at command time, since `merge_into` is only reached once CI is green). One comment is posted to the PR the first time it waits. - **Force merge.** `-f` gets no wait window and therefore never waits; it refuses instead if greenlight is the only authority and hasn't verified the head. - **ghstack.** Checked against each open PR's GitHub head, which is what greenlight records, not the `/orig` revision git cherry-picks. - **Failure modes.** A HUD outage is a wait, not a refusal, until the budget runs out; an unreadable `merge_rules.yaml` or an unclassifiable reviewer leaves the guard on rather than inventing an authorization. Depends on the `/api/greenlight/pr_state` route in pytorch/test-infra; `HUD_API_TOKEN` is already plumbed into `trymerge.yml`. This is the last change of the guard and the only one that alters existing behaviour; the modules it calls landed ahead of it so that the merge-path wiring could be reviewed on its own and each change stay under the 2000 LOC infra limit. One test here asserts `merge_rules.yaml` still grants `pytorchgreenlight` merge authority, so a rename there cannot silently disable the guard. Signed-off-by: Jean Schmidt <[email protected]> Pull Request resolved: pytorch#194841 Approved by: https://github.com/huydhn ghstack dependencies: pytorch#194838, pytorch#194839, pytorch#194840
Stack from ghstack (oldest at bottom):
greenlight_guard.pyis the decision itself. It compares the head SHA greenlightrecorded in its ledger against the head actually being landed and returns ALLOW, WAIT or
DENY, along with the wait budget (
GreenlightWaitWindow) a merge carries acrossre-entries. Everything it needs about a PR arrives as plain data or as a callable on
PRUnderMerge— the callables stay callables because each one costs a GitHub requestthat only the minority of PRs greenlight alone authorizes ever needs. It must not import
trymerge:trymergeimports it, and the cycle would break both.Two failure directions are split deliberately. Anything that is only unknown — no ledger
row, a verdict for an older commit, a review still running, an unreadable HUD, a reviewer
GitHub would not classify — waits, and is refused only once the budget is spent. Anything
settled against the head being landed — NO_LAND/CANCELLED/FAILED, an unrecognized status,
a review that has outlived greenlight's own timeout — refuses at once. Refusing on first
uncertainty would turn a HUD blip into a dead merge; waiting on a settled negative would
burn an hour and land nothing.
The tests cover the decision table, stack handling, the laziness of the per-PR GitHub
lookups, the ledger's transport failure modes against a mocked
gh_fetch_url(everydocumented route error code, malformed payloads, batch capping, backoff, recovery on
retry), and the preflight mirror. No test touches the network. They land together with
the guard because
test_greenlight_guard.pyowns the shared PR fixtures that the ledgerand preflight tests build on.
Split out from the
trymerge.pywiring so the verdict logic can be reviewed againstgreenlight's behaviour independently of the merge path, and to keep each change under the
2000 LOC infra limit. Nothing calls into this yet.
Signed-off-by: Jean Schmidt [email protected]