potiuk commented on code in PR #77101:
URL: https://github.com/apache/airflow/pull/77101#discussion_r4748100797


##########
dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py:
##########
@@ -3262,15 +3296,39 @@ def is_gated_base(base_ref: str) -> bool:
                 )
             ):
                 plan.ci_passed = None
-            draft_change = apply_plan(client, pr, plan)
+            # A newly authorized PR gets project CI from the original gated 
run. The lookup
+            # checks the latest run's attempt, so the next reconciliation does 
not request it again.
+            release_run = (

Review Comment:
   **The rerun starts project CI without a maintainer's approval**
   
   `decide_gated_approval` (`workflow_run_approval.py`) approves a gated pull 
request's run on the premise that "its project CI run only executes the gate", 
without the dependency, size and job-minute checks `decide_approval` applies. 
Before this change that run was deleted, so `use project ci` took effect on a 
new run that entered `action_required` for authors the approval setting covers. 
Rerunning the already-approved run with the monitor's `actions: write` token 
does not re-enter approval, and the gate now reads `use project ci` and runs 
the full matrix. The override labels can be set by anyone with triage rights 
(`08_fork_ci_for_external_contributors.md`, "Override labels"), who cannot 
approve fork runs, and the doc text this PR edits still says the run waits for 
a maintainer's approval. Either keep project CI behind approval (e.g. only 
rerun when the author is not covered by the approval setting, or re-run through 
the approval path that applies `decide_approval`), or make it an 
 explicit, documented decision that adding `use project ci` is the approval, 
and then restrict who may add it.
   
   *Flagged by both the primary and adversarial reviewers.*
   
   ---
   Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting



##########
dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py:
##########
@@ -2872,6 +2903,9 @@ def apply_plan(client: GitHubClient, pr: 
PullRequestSnapshot, plan: Plan) -> Dra
     # They carry no marker, so the state comment stays the earliest bot 
comment with one.
     for comment in plan.new_comments:
         client.create_comment(pr.number, comment)
+    # A failed or rejected rerun leaves `fork ci required` and the old marker 
in place for retry.
+    if rerun_run_id is not None:
+        client.rerun_run(pr.number, rerun_run_id)

Review Comment:
   **A rerun GitHub refuses blocks the release on every pass**
   
   The rerun runs inside `apply_plan` before `fork ci required` is removed, and 
a failure is meant to be retried. But a refusal that does not go away (a run 
older than GitHub's 30-day rerun window, which a pull request open that long 
reaches; a permission problem) raises a `GitHubError` that `run_reconcile` 
records under `unknown`, and on the next pass the same run is still a completed 
first attempt, so it is picked again and refused again. The pull request is 
never released and is reported every five minutes; before this change adding 
`use project ci` always released it. Because the call sits before the status 
updates, the refusal also skips moving a green fork `CI passed` back to 
pending. Treat a refused rerun as "no run to rerun": release anyway and fall 
back to the next push, and only retry for errors that can be transient. Add a 
test where `rerun_run` keeps failing.
   
   *Flagged by both the primary and adversarial reviewers.*
   
   ---
   Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting



##########
dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py:
##########
@@ -2367,6 +2367,35 @@ def find_gated_runs(self, pr: PullRequestSnapshot) -> 
list[WorkflowRun]:
             and self._commit_changes_workflows(run, pr.base_ref) is False
         ]
 
+    def find_latest_gated_run(self, pr: PullRequestSnapshot) -> WorkflowRun | 
None:
+        """Find the current commit's original gated run, even after the 
tampering window."""
+        if not pr.head_repo or not pr.head_ref:
+            return None
+        path = (
+            
f"repos/{self._repo}/actions/workflows/{CI_WORKFLOW_FILE}/runs?event=pull_request"
+            f"&branch={quote(pr.head_ref, 
safe='')}&head_sha={pr.head_sha}&per_page=100"
+        )
+        runs = [
+            run
+            for raw in self._client.rest_get(path).get("workflow_runs", [])
+            if (run := parse_workflow_run(raw)).head_repo.casefold() == 
pr.head_repo.casefold()

Review Comment:
   **Run selection is not tied to this pull request**
   
   `find_latest_gated_run` matches the fork repository, branch and head SHA, 
but not the pull request number or base. Two pull requests from one branch 
(e.g. to `main` and to a release branch) share those, so the newest run can 
belong to the other one; its rerun keeps that event's `PR_NUMBER`, the gate 
reads that pull request's labels, gates again, and this pull request is 
released with its retry label removed and no project CI started. A run from 
before the base was changed is picked the same way. Match on the run's pull 
request number (from the event payload, as the gate does) and drop runs older 
than the last base change.
   
   *Flagged by both the primary and adversarial reviewers.*
   
   ---
   Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting



##########
dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py:
##########
@@ -2367,6 +2367,35 @@ def find_gated_runs(self, pr: PullRequestSnapshot) -> 
list[WorkflowRun]:
             and self._commit_changes_workflows(run, pr.base_ref) is False
         ]
 
+    def find_latest_gated_run(self, pr: PullRequestSnapshot) -> WorkflowRun | 
None:
+        """Find the current commit's original gated run, even after the 
tampering window."""
+        if not pr.head_repo or not pr.head_ref:
+            return None
+        path = (
+            
f"repos/{self._repo}/actions/workflows/{CI_WORKFLOW_FILE}/runs?event=pull_request"
+            f"&branch={quote(pr.head_ref, 
safe='')}&head_sha={pr.head_sha}&per_page=100"
+        )
+        runs = [
+            run
+            for raw in self._client.rest_get(path).get("workflow_runs", [])
+            if (run := parse_workflow_run(raw)).head_repo.casefold() == 
pr.head_repo.casefold()
+            and run.head_branch == pr.head_ref
+            and run.head_sha == pr.head_sha
+            and run.event == "pull_request"
+            and run.workflow_file == CI_WORKFLOW_FILE
+        ]
+        latest = max(runs, key=lambda run: (run.created_at, run.id), 
default=None)
+        if (
+            latest is None
+            or latest.status != "completed"

Review Comment:
   **A release while the gated run is still finishing loses the rerun**
   
   `find_latest_gated_run` returns None until the run is `completed`, but the 
release still goes ahead and removes `fork ci required`, which is what selects 
the pull request for later passes. A pull request released while its gated 
run's skipped jobs are still finishing therefore never gets the rerun and needs 
a push. Defer the release (keep the label) while the head's gated run is still 
in progress, or let a later pass find released pull requests that still have an 
un-rerun gated run.
   
   *Flagged by codex (adversarial review); cross-checked.*
   
   ---
   Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting



##########
dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py:
##########
@@ -3262,15 +3296,39 @@ def is_gated_base(base_ref: str) -> bool:
                 )
             ):
                 plan.ci_passed = None
-            draft_change = apply_plan(client, pr, plan)
+            # A newly authorized PR gets project CI from the original gated 
run. The lookup
+            # checks the latest run's attempt, so the next reconciliation does 
not request it again.
+            release_run = (
+                bypass_finder.find_latest_gated_run(pr)

Review Comment:
   **The rerun uses the snapshot's head without re-checking it**
   
   The release pass reruns the run for `pr.head_sha` from the snapshot fetched 
at the start of the reconciliation, without the live head check other mutating 
paths make. If the author pushed meanwhile, project CI for the new commit may 
already be running under `use project ci`, and `ci-amd.yml`'s per-PR 
concurrency group with `cancel-in-progress` lets the rerun of the old commit 
cancel it. Re-read the pull request's head (as 
`is_unchanged_without_project_ci` does) and skip the rerun when it moved.
   
   *Flagged by antigravity (adversarial review); cross-checked.*
   
   ---
   Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting



##########
dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py:
##########
@@ -3262,15 +3296,39 @@ def is_gated_base(base_ref: str) -> bool:
                 )
             ):
                 plan.ci_passed = None
-            draft_change = apply_plan(client, pr, plan)
+            # A newly authorized PR gets project CI from the original gated 
run. The lookup
+            # checks the latest run's attempt, so the next reconciliation does 
not request it again.
+            release_run = (
+                bypass_finder.find_latest_gated_run(pr)
+                if plan.state == ForkState.RELEASED
+                and LABEL_USE_PROJECT_CI in pr.labels
+                and LABEL_FORK_CI_REQUIRED in pr.labels
+                else None
+            )
+            draft_change = apply_plan(
+                client, pr, plan, rerun_run_id=release_run.id if release_run 
is not None else None
+            )
             if draft_change is not None:
                 draft_changes.append(draft_change)
             # The fork's result is in the `Fork CI / Tests (AMD)` status; the 
gated run's skipped
             # checks, and the cancelled ones of a run a newer push cancelled, 
only clutter the pull
             # request. Runs with any project CI work stay for the tampering 
check.
             if plan.state != ForkState.GATE_BYPASSED:
-                for run in bypass_finder.find_gated_runs(pr):
-                    client.delete_run(pr.number, run.id)
+                gated_runs = bypass_finder.find_gated_runs(pr)
+                # Keep the newest original gated run of this head until 
release. Historical
+                # gate-only runs are still removed to avoid cluttering the 
check list.
+                retained = (

Review Comment:
   **The retained run can be a cancelled one**
   
   `find_gated_runs` returns gated skip runs and cancelled gate runs; the 
retention keeps the newest run of the head of either kind and deletes the rest. 
A later duplicate event for the same commit (`ready_for_review` after `opened`) 
that was cancelled is kept and the valid gated run deleted, and 
`find_latest_gated_run` then refuses the cancelled one. Retain only a run for 
which `is_gated_skip_run` holds.
   
   *Flagged by antigravity (adversarial review); cross-checked.*
   
   ---
   Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting



##########
dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py:
##########
@@ -2872,6 +2903,9 @@ def apply_plan(client: GitHubClient, pr: 
PullRequestSnapshot, plan: Plan) -> Dra
     # They carry no marker, so the state comment stays the earliest bot 
comment with one.
     for comment in plan.new_comments:
         client.create_comment(pr.number, comment)
+    # A failed or rejected rerun leaves `fork ci required` and the old marker 
in place for retry.
+    if rerun_run_id is not None:
+        client.rerun_run(pr.number, rerun_run_id)

Review Comment:
   **Selective checks see the labels the original run started with**
   
   A rerun replays the original `pull_request` event, and `build-info` derives 
`PR_LABELS` from the payload, so labels added together with `use project ci` 
(`full tests needed`, `all versions`, ...) do not affect the selection, as 
`workflow_run_approval.py` notes for re-runs. Worth a sentence in the docs, 
since adding such labels is one of the stated reasons for `use project ci`. 
Document it, or let selective checks read the labels live like the gate.
   
   *Flagged by antigravity (adversarial review); cross-checked.*
   
   ---
   Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to