Skip to content

[trymerge] Verify greenlight approved the exact commit being landed - #194841

Closed
jeanschmidt wants to merge 4 commits into
gh/jeanschmidt/4/basefrom
gh/jeanschmidt/4/head
Closed

jeanschmidt wants to merge 4 commits into
gh/jeanschmidt/4/basefrom
gh/jeanschmidt/4/head

Conversation

@jeanschmidt

@jeanschmidt jeanschmidt commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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]

[ghstack-poisoned]
@jeanschmidt
jeanschmidt requested a review from a team as a code owner August 26, 2026 01:03
@pytorch-bot

pytorch-bot Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 Cancelled Job, 40 Pending, 2 Unrelated Failures

As of commit 018fd65 with merge base e812f6f (image):

GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:

This arms a fail-closed gate in the live merge path:
check_greenlight_reviewed_head_sha now runs on every merge_into
(trymerge.py:1504-1513), and for a PR authorized only by merge_rules.yaml's
'Greenlight Review Bot' rule (merge_rules.yaml:636) an unreadable ledger becomes
WAIT and then a hard DENY once GreenlightWaitWindow's 60-minute budget is spent
(greenlight_guard.py:308-313), with force merges (greenlight_wait=None) refusing
immediately; correctness therefore depends on the /api/greenlight/pr_state HUD
route (greenlight_ledger.py:43) that the PR body says needs
pytorch/test-infra#8630 to land first, a cross-repo ordering that cannot be
verified from this checkout. Separately, the extracted rule_approvers
(trymerge.py:1731-1747) now raises an uncaught ValueError from inside
find_matching_merge_rule on a multi-slash team ref, changing the failure mode of
a function whose own NB marks it as used by Meta-internal workflows. Maintainer
huydhn left a design objection on 2026-08-27 asking why this belongs in mergebot
rather than stripApprovalBot and closed with "Let's chat more on this tmrw"; the
only record of resolution is the author's own summary of an offline chat, with
no maintainer sign-off in the thread, so a human should confirm before this
lands.

reason: build_or_ci_risk
Reviewed commit: e276805 (NOT the current head 018fd65)

Inference job

CANCELLED JOB - The following job was cancelled. Please retry:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

@jeanschmidt

Copy link
Copy Markdown
Contributor Author

Full battery of tests (visible internal only) P2477157757

if state == "CHANGES_REQUESTED"
]

def get_updated_at(self) -> str | None:

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.

From what I see, we need 2+ additional requests here to get the updated_at timestamp and the list of reviewers. Let's check which token we are using and how much limit is left there

@jeanschmidt jeanschmidt Aug 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, but this is only at try-merge, so it is 2 additional requests, for each merge attempt. With 40 merges a day, those are 80 extra requests a day, the budget is 12.5k/hour, this is 0.026% of our daily budget

@huydhn

huydhn commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

My understand here is that all these changes is to add a land-time guard in merge_into to compare the head SHA greenlight actually evaluated against the head being landed, and refuse unless greenlight recorded LAND for that exact commit. The question that I'm trying to answer is why we need to do it here, in mergebot. On the one hand, it's the right place to implement this change if that's the route we want. On the other hand, it's mergebot, so only working for pt/pt and needs lots of setup code to work.

There are 2 groups of users I have in mind:

  1. Those with write permission or trusted contributors. Why don't we strip greenlight approval and ask it to re-review their PRs when they are updated? Why do we need to go the extra length here? There hasn't been an example of a trusted contributor abusing their rights till date. This is what we have today.
  2. Those without write permission and can't be trusted. Why don't we just revoke any approvals right away if their PRs are updated, both human and greenlight? We have https://github.com/pytorch/test-infra/blob/main/torchci/lib/bot/stripApprovalBot.ts for this. In addition, greenlight approval alone means nothing if lint and pull are not run, they are mandatory checks and non-write users can't force merge. So, a reviewer needs to come in and approve CI run. This also acts as a mechanism to protect against fail open.

In both cases, greenlight can review the change again after stripApprovalBot removes the previous approvals and mergebot is not involved which simplifies the setup. Are there any pt-specific use cases that I miss here that we couldn't do without mergebot? I would argue that pt shouldn't be treated differently w.r.t greenlight. Let's chat more on this tmrw.

@jeanschmidt

Copy link
Copy Markdown
Contributor Author

@huydhn lets chat about it

@jeanschmidt

Copy link
Copy Markdown
Contributor Author

updating discussions with @huydhn

We reached an agreement that this introduction of functionality on try-merge / pytorchbot is primarily intended to reduce the noise experienced by users while developing in PyTorch, due to the noise after submission of revoking and then re-approving. And as a secondary benefit, this also provides stronger guarantees against a landing of changes that are not yet approved and could be revoked by GreenLight. But we both agree that this advantage is minor on its own.

And exactly because of that, GreenLight would behave differently for repositories with DrCI/pytorchbot vs without.

With DrCI + pytorchbot:

  • Reduced noise:
    • by using DrCI messages for reporting bot status
    • by using pytorchbot to validate merges

Without DrCI + pytorchbot

  • Increased noise:
    • greenlight with its own comment to report status
    • revoking the GreenLight approval at new submission

@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!

Comment thread .github/scripts/trymerge.py Outdated
traceback.print_exc()
return frozenset()
return frozenset(
normalize_login(login) for rule in rules for login in rule_approvers(rule)

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.

Nit: rule_approvers calls gh_get_team_members and it's a GraphQL request. We should have it inside the try statement

[ghstack-poisoned]
jeanschmidt added a commit that referenced this pull request Aug 27, 2026
**Impact:** mergebot only — PRs whose sole merge authorization is the `pytorchgreenlight`
approval
**Risk:** medium

## 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]>
ghstack-source-id: 6b2659e
Pull-Request: #194841
jeanschmidt added a commit that referenced this pull request Aug 27, 2026
**Impact:** mergebot only — PRs whose sole merge authorization is the `pytorchgreenlight`
approval
**Risk:** medium

## 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]>
ghstack-source-id: 6b2659e
Pull-Request: #194841
[ghstack-poisoned]
jeanschmidt added a commit that referenced this pull request Aug 27, 2026
**Impact:** mergebot only — PRs whose sole merge authorization is the `pytorchgreenlight`
approval
**Risk:** medium

## 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]>
ghstack-source-id: 6df4b4d
Pull-Request: #194841
[ghstack-poisoned]
jeanschmidt added a commit that referenced this pull request Aug 28, 2026
**Impact:** mergebot only — PRs whose sole merge authorization is the `pytorchgreenlight`
approval
**Risk:** medium

## 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]>
ghstack-source-id: 938a301
Pull-Request: #194841
@jeanschmidt

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f "lintrunner is green on top PR, all the other CI should not be testing try-merge behavior. Should be ready to merge"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…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
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…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
@github-actions
github-actions Bot deleted the gh/jeanschmidt/4/head branch November 11, 2026 01:42
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 topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants