From c58af4e1d568ca9fc062f38e73cfd91332d99099 Mon Sep 17 00:00:00 2001 From: Danathar <6772335+Danathar@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:42:18 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20adoption:=20keep=20a=20same-?= =?UTF-8?q?repo=20PR=20in=20place=20across=20a=20base=20workflow=20change?= =?UTF-8?q?=20when=20the=20App=20can=20push=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit canPushToPRBranch ran baseSyncTouchesWorkflows before its same-repo check, and baseSyncTouchesWorkflows assumed the App can never push a base-sync that carries a .github/workflows/ change. That holds for a fork head (the fork is not in the installation), but on a branch of the base repository the push lands once the installation grants workflows: write. So the hive superseded same-repo PRs it could have adopted in place, closing them and losing their approvals (projectbluefin/common#1689 -> #1708, #1690 -> #1710). A same-repo head now supersedes on a base workflow change only when hgithub.Client.WorkflowsPushable is false (no grant, or no trusted or merger lane serving the repo). Fork heads and compare failures behave as before. Adopted-PR recovery also logs a refused update-branch with the merge sweep's reasons (fork-workflow-update, workflow-permission) instead of the catch-all update-branch-failed, which is what projectbluefin/utah-packages#1173 and projectbluefin/actions#752 showed: both are fork heads that no App grant can update. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> --- .../fixed-adopt-in-place-workflows-grant.md | 1 + src/docs/hive-merge.md | 6 +- .../stage-labels.md | 14 +- .../automerge_adopt_workflows_grant_test.go | 165 ++++++++++++++++++ .../automerge/automerge_adoption_recovery.go | 31 +++- .../automerge/automerge_contributor_adopt.go | 51 ++++-- 6 files changed, 241 insertions(+), 27 deletions(-) create mode 100644 changelog.d/fixed-adopt-in-place-workflows-grant.md create mode 100644 src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go diff --git a/changelog.d/fixed-adopt-in-place-workflows-grant.md b/changelog.d/fixed-adopt-in-place-workflows-grant.md new file mode 100644 index 0000000000..d408004d21 --- /dev/null +++ b/changelog.d/fixed-adopt-in-place-workflows-grant.md @@ -0,0 +1 @@ +- Adoption no longer closes and re-opens a contributor PR whose branch lives in the base repository just because the base changed a file under `.github/workflows/`. The hive used to assume the App can never push such a base-sync and superseded the PR, which dropped its approvals and number (projectbluefin/common#1689 became #1708). Now it checks the installation: when the App installation grants `workflows: write` and a trusted- or merger-tier lane serves the repository, the PR is adopted in place on its own branch. Fork PRs are unchanged, because GitHub refuses any App workflow-file push to a fork whatever the base installation grants. A refused `update-branch` during adopted-PR recovery now logs `fork-workflow-update` or `workflow-permission` instead of the catch-all `update-branch-failed`. diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 77678f1259..e5157f0244 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -1728,7 +1728,11 @@ maintainers may edit) or re-runs the failed workflow jobs when it is current, at most once per (head, base tip) and three times per adoption, logged as `adopted PR recovery` with `reason=update-branch`, `rerun-failed-jobs` or `skipped:` (`skipped:dependency-bot-branch` for a behind `renovate/*` or -dependency-bot branch, which is never updated); an escalation already in place for that head is released +dependency-bot branch, which is never updated; `skipped:fork-workflow-update` +when the base changed `.github/workflows/` and the head is in a fork, which +GitHub refuses to update for any App whatever the base installation grants; +`skipped:workflow-permission` when a branch of the base repository was refused +because the token lacks `workflows: write`); an escalation already in place for that head is released (hivecommons/hive#14500). An adoption with nothing the hive can push past the dwell — after that recovery was tried and the head is still red — goes to `stage/085-needs-decision` as diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index f6497a4349..c7b5422829 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -1228,7 +1228,12 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human- branch (`governor.labels.stage.adoption.in_place`, default `true`): - **in place** (`mode=in-place`) — the head is in the base repo, or the fork allows edits by maintainers (`maintainer_can_modify`, resolved with - one `GET pulls/N` at adoption time): the hive posts ONE + one `GET pulls/N` at adoption time). If the base changed a file under + `.github/workflows/` since the PR's merge-base, a head in the base repo + stays in place only when the App installation grants `workflows: write` + and a lane whose token asks for it (trusted or merger tier) serves the + repo, so the base-sync can be pushed; a fork head never does (see + above). The hive posts ONE `` comment, moves the PR to `stage/074-adopted-by-hive` and files it in `ci-failing.json` as its own (`"adopted": true`, `reachable_action: @@ -1349,8 +1354,11 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human- over. A pending `tide` or missing `lgtm` is never the reason — the lane has not admitted the head; the comment says so. - **supersede** (`mode=supersede`) — the fork forbids maintainer edits, - `in_place: false`, or the adopted head has unsigned commits (`no-dco`) - that the hive cannot repair by amending the contributor branch: + `in_place: false`, the base-sync of a head in the base repo carries a + `.github/workflows/` change the hive cannot push (no `workflows: write` + grant, or the head...base compare failed), or the adopted head has + unsigned commits (`no-dco`) that the hive cannot repair by amending the + contributor branch: - pushes `hive/adopt-` in the base repo at the contributor's head SHA (the fork's commits, authorship and `Signed-off-by` preserved byte for byte — "sign-off survives the squash"); diff --git a/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go b/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go new file mode 100644 index 0000000000..be2c333136 --- /dev/null +++ b/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go @@ -0,0 +1,165 @@ +package automerge + +import ( + "context" + "errors" + "net/http" + "strings" + "sync" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +// workflowsGrantTransport is the engine's client with a fixed answer to +// WorkflowsPushable: an installation that grants (or lacks) `workflows: +// write` with a lane that asks for it serving the repo. +type workflowsGrantTransport struct { + *hgithub.Client + pushable bool + + mu sync.Mutex + asked []string +} + +func (t *workflowsGrantTransport) WorkflowsPushable(_ context.Context, repo string) bool { + t.mu.Lock() + defer t.mu.Unlock() + t.asked = append(t.asked, repo) + return t.pushable +} + +func (t *workflowsGrantTransport) askedRepos() []string { + t.mu.Lock() + defer t.mu.Unlock() + return append([]string(nil), t.asked...) +} + +func withWorkflowsGrant(t *testing.T, h *adoptHarness, pushable bool) *workflowsGrantTransport { + t.Helper() + client, ok := h.e.transport.(*hgithub.Client) + if !ok { + t.Fatalf("engine transport is %T, want *hgithub.Client", h.e.transport) + } + tr := &workflowsGrantTransport{Client: client, pushable: pushable} + h.e.transport = tr + return tr +} + +// A base that changed .github/workflows/ supersedes a branch of the base +// repository only when the hive cannot push workflow files there. With the +// installation's `workflows: write` grant, the base-sync lands on the PR's +// own branch, so the PR is adopted in place and keeps its number and +// approvals (projectbluefin/common#1689 was closed and re-opened as #1708). +// A compare failure still supersedes. +func TestAdoptionInPlaceWorkflowGuardHonoursGrant(t *testing.T) { + cases := []struct { + name string + pushable bool + files []string + failCompare bool + wantInPlace bool + wantAsked bool + }{ + {name: "grant: same-repo head adopted in place", pushable: true, files: []string{".github/workflows/build.yml"}, wantInPlace: true, wantAsked: true}, + {name: "no grant: same-repo head superseded", files: []string{".github/workflows/build.yml"}, wantAsked: true}, + {name: "grant: compare error still supersedes", pushable: true, failCompare: true}, + {name: "grant: no workflow change needs no grant read", pushable: true, files: []string{"src/a.go"}, wantInPlace: true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + tr := withWorkflowsGrant(t, h, tc.pushable) + h.adopt.allowMaintainerEdits(7, true) + h.adopt.compareFiles = tc.files + h.adopt.failCompare = tc.failCompare + h.observe(adoptPR(7, "aaa"), "check-failing:unit") + labels := []string{"stage/072-returned-to-contributor", "role/contributor", "author/clanker"} + h.clock.advance(30 * time.Minute) + h.observe(adoptPR(7, "aaa", labels...), "check-failing:unit") + comments := adoptionComments(h.api) + if len(comments) != 1 { + t.Fatalf("adoption comments: %v", h.api.comments) + } + if got := strings.Contains(comments[0], "mode=in-place"); got != tc.wantInPlace { + t.Fatalf("in-place = %v, want %v: %s", got, tc.wantInPlace, comments[0]) + } + if superseded := len(h.adopt.pulls) == 1; superseded == tc.wantInPlace { + t.Fatalf("superseding PRs = %d, in-place want %v", len(h.adopt.pulls), tc.wantInPlace) + } + asked := tr.askedRepos() + if (len(asked) > 0) != tc.wantAsked { + t.Fatalf("grant read for %v, want asked=%v", asked, tc.wantAsked) + } + for _, repo := range asked { + if repo != "acme/widget" { + t.Fatalf("grant read for %q, want acme/widget", repo) + } + } + }) + } +} + +// A fork branch takes no workflow-file push from the App whatever the base +// installation grants (projectbluefin/utah-packages#1173, actions#752), so +// the grant never makes a fork PR adoptable in place across a base workflow +// change: it stays with its author and is not copied into the base repo. +func TestAdoptionForkWorkflowGuardIgnoresGrant(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + tr := withWorkflowsGrant(t, h, true) + h.adopt.allowMaintainerEdits(7, true) + h.adopt.compareFiles = []string{".github/workflows/build-stage.yml"} + h.observe(forkPR(7, "aaa"), "check-failing:unit") + labels := []string{"stage/072-returned-to-contributor", "role/contributor", "author/clanker"} + h.clock.advance(30 * time.Minute) + h.observe(forkPR(7, "aaa", labels...), "check-failing:unit") + if c := adoptionComments(h.api); len(c) != 0 { + t.Fatalf("fork PR adopted across a base workflow change: %v", c) + } + if len(h.adopt.pulls) != 0 || len(h.adopt.refs) != 0 { + t.Fatalf("fork code copied into the base repository: pulls=%v refs=%v", h.adopt.pulls, h.adopt.refs) + } + if rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)); !rec.ForkRehomeRefused("aaa") { + t.Fatalf("fork head not recorded as staying with its author: %+v", rec) + } + if asked := tr.askedRepos(); len(asked) != 0 { + t.Fatalf("fork head read the base installation's grant: %v", asked) + } +} + +// A refused update-branch on an adopted head is logged with the merge +// sweep's own reason, not the catch-all. +func TestAdoptionRecoveryUpdateBranchSkip(t *testing.T) { + cases := []struct { + name string + status int + msg string + fork bool + want string + }{ + {name: "fork head, base changed workflows", status: http.StatusForbidden, msg: workflowPermissionRefusal, fork: true, want: "fork-workflow-update"}, + {name: "same-repo head, token lacks workflows", status: http.StatusForbidden, msg: workflowPermissionRefusal, want: "workflow-permission"}, + {name: "fork not editable", status: http.StatusUnprocessableEntity, msg: "maintainer edits are not allowed", fork: true, want: "fork-not-editable"}, + {name: "conflict", status: http.StatusUnprocessableEntity, msg: "merge conflict between base and head", want: "conflict"}, + {name: "server error", status: http.StatusBadGateway, msg: "bad gateway", want: "update-branch-failed"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := &gh.ErrorResponse{Response: &http.Response{StatusCode: tc.status}, Message: tc.msg} + if got := adoptionRecoveryUpdateBranchSkip(classifyUpdateBranchError(err, tc.fork)); got != tc.want { + t.Fatalf("skip reason %q, want %q", got, tc.want) + } + }) + } + if got := adoptionRecoveryUpdateBranchSkip(classifyUpdateBranchError(errors.New("network"), false)); got != "update-branch-failed" { + t.Fatalf("plain error: %q", got) + } +} diff --git a/src/pkg/github/automerge/automerge_adoption_recovery.go b/src/pkg/github/automerge/automerge_adoption_recovery.go index a7ecc2599b..f55b38a95c 100644 --- a/src/pkg/github/automerge/automerge_adoption_recovery.go +++ b/src/pkg/github/automerge/automerge_adoption_recovery.go @@ -292,17 +292,11 @@ func (c *Engine) recoverByUpdateBranch(ctx context.Context, store *stagelabels.S if wait := preparationWaitReason(err); wait != "" { return adoptionRecovery{Deferred: true, Reason: adoptionRecoverySkipPrefix + wait}, true } - var out adoptionRecovery - switch classifyUpdateBranchError(err, fork) { - case updateBranchUpToDate: + class := classifyUpdateBranchError(err, fork) + if class == updateBranchUpToDate { return adoptionRecovery{}, false - case updateBranchForkNotEditable: - out = adoptionRecoverySkipped("fork-not-editable") - case updateBranchConflict: - out = adoptionRecoverySkipped("conflict") - default: - out = adoptionRecoverySkipped("update-branch-failed") } + out := adoptionRecoverySkipped(adoptionRecoveryUpdateBranchSkip(class)) c.warn("adopted PR recovery", append(logArgs, "reason", out.Reason, "error", err)...) return out, true } @@ -317,6 +311,25 @@ func (c *Engine) recoverByUpdateBranch(ctx context.Context, store *stagelabels.S return adoptionRecovery{Attempted: true, Reason: adoptionRecoveryUpdateBranch}, true } +// adoptionRecoveryUpdateBranchSkip names why a refused update-branch left +// the adopted head as it was. A workflow-file refusal gets the merge +// sweep's own reason (fork-workflow-update on a fork head the App cannot +// push workflow files to, workflow-permission on a branch of the base +// repository when the token lacks the grant), not the catch-all. +func adoptionRecoveryUpdateBranchSkip(class updateBranchFailure) string { + switch class { + case updateBranchForkNotEditable: + return "fork-not-editable" + case updateBranchConflict: + return "conflict" + case updateBranchForkWorkflowUpdate: + return autoMergeReasonForkWorkflowUpdate + case updateBranchWorkflowPermission: + return autoMergeReasonWorkflowPermission + } + return "update-branch-failed" +} + // recoverByRerun re-runs the failed jobs of the latest failed run of every // workflow on the adopted head. func (c *Engine) recoverByRerun(ctx context.Context, obs stageObservation, logArgs []any) adoptionRecovery { diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index 6c2a5db8e7..7a16be2bff 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -2152,10 +2152,11 @@ func (c *Engine) canPushToPRBranch(ctx context.Context, obs stageObservation) bo if obs.HeadRepo == "" { return false } - if c.baseSyncTouchesWorkflows(ctx, obs) { + sameRepo := strings.EqualFold(obs.HeadRepo, obs.Owner+"/"+obs.Repo) + if c.baseSyncTouchesWorkflows(ctx, obs, sameRepo) { return false } - if strings.EqualFold(obs.HeadRepo, obs.Owner+"/"+obs.Repo) { + if sameRepo { return true } pr, _, err := c.gh.PullRequests.Get(ctx, obs.Owner, obs.Repo, obs.Number) @@ -2182,24 +2183,46 @@ func (c *Engine) pushAccessLost(ctx context.Context, obs stageObservation) bool } // baseSyncTouchesWorkflows reports whether the base has moved past a change -// under .github/workflows/ since the PR's merge-base. Pushing such a -// base-sync to a contributor's fork branch is refused for an App token -// without the `workflows` grant (#6985; an installation with Workflows: -// Read and write can merge approved workflow-path PRs into the base, #14664, -// but a fork push is a separate matter), so in-place adoption would be a -// dead end (#12865). A compare failure counts as touching: a superseding PR -// is always deliverable. -func (c *Engine) baseSyncTouchesWorkflows(ctx context.Context, obs stageObservation) bool { +// under .github/workflows/ since the PR's merge-base that the hive cannot +// push into the PR's branch, so in-place adoption would be a dead end +// (#12865). GitHub refuses an App push that creates or updates a workflow +// file without the `workflows` grant (#6985). On a contributor's fork no +// grant on the base repository helps: the fork is not part of the +// installation (projectbluefin/testsuite#1037). On a branch of the base +// repository the push lands when the installation grants `workflows: write` +// and a lane whose token asks for it serves the repo (#4677, #14664; +// hgithub.Client.WorkflowsPushable), so sameRepo heads supersede only when +// that grant is missing or unknown. A compare failure counts as touching: a +// superseding PR is always deliverable. +func (c *Engine) baseSyncTouchesWorkflows(ctx context.Context, obs stageObservation, sameRepo bool) bool { file, err := c.baseSyncWorkflowFile(ctx, obs) if err != nil { c.warn("stage labels: adoption could not compare head to base for workflow changes; superseding", "repo", obs.Display, "pr", obs.Number, "error", err) return true } - if file != "" { - c.info("stage labels: base-sync touches .github/workflows/ which the App cannot push; superseding", "repo", obs.Display, "pr", obs.Number, "file", file) - return true + if file == "" { + return false } - return false + if sameRepo && c.workflowsPushable(ctx, obs.Owner+"/"+obs.Repo) { + c.info("stage labels: base-sync touches .github/workflows/ and the installation grants workflows: write; keeping the PR's own branch", "repo", obs.Display, "pr", obs.Number, "file", file) + return false + } + c.info("stage labels: base-sync touches .github/workflows/ which the App cannot push; superseding", "repo", obs.Display, "pr", obs.Number, "file", file, "same_repo", sameRepo) + return true +} + +// workflowsPushableTransport is the optional transport hook that reports +// whether the hive can push .github/workflows/** in a repository: the App +// installation grants `workflows: write` and a lane whose token asks for it +// serves the repo (hgithub.Client.WorkflowsPushable). Unknown reads false. +type workflowsPushableTransport interface { + WorkflowsPushable(ctx context.Context, repo string) bool +} + +// workflowsPushable reports the transport's answer, false when it has none. +func (c *Engine) workflowsPushable(ctx context.Context, repo string) bool { + t, ok := c.transport.(workflowsPushableTransport) + return ok && t.WorkflowsPushable(ctx, repo) } // baseSyncWorkflowFile returns the first file under .github/workflows/ that From 3303c0f8001a115e335f9dd0797635f80c0ff892 Mon Sep 17 00:00:00 2001 From: Danathar <6772335+Danathar@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:51:47 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20adoption:=20gate=20in-place?= =?UTF-8?q?=20workflow=20base-sync=20on=20the=20fixer's=20own=20token,=20n?= =?UTF-8?q?ot=20any=20lane?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review of the previous commit: WorkflowsPushable asks whether any trusted or merger lane serves the repo, but an adopted head that conflicts (or stays red) is pushed by the fallback fixer that routeConflictFixAgent picks, scanner. A hive with ci-maintainer at trusted and scanner at contributor tier passed the gate, adopted in place, and then scanner's push was refused, the #12865 dead end. - pkg/github: FallbackFixAgent is the one name for that fixer, used by routeConflictFixAgent and the adoption sweep. AgentWorkflowsPushable reports an agent's own capability: its tier asks for Workflows (SetWorkflowsSelfPushFunc) and the installation grants workflows: write. - automerge: baseSyncTouchesWorkflows keeps a same-repo head in place only when AgentWorkflowsPushable(FallbackFixAgent) holds. The comment now says the premise of #12865 changed (#14664 found the grant; pr_merge_state.go fences only forks). - Tests: TestAgentWorkflowsPushable for the real predicate (contributor fixer with the grant, trusted fixer, token client, read-only grant, failed read); TestAdoptedRecoveryForkWorkflowRefusalNamed drives recoverByUpdateBranch end to end on a fork head. - Docs and changelog name the fixer condition and the DCO in-place repair it also unlocks. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> --- .../fixed-adopt-in-place-workflows-grant.md | 2 +- src/cmd/hive/merge_eligibility.go | 6 +- .../stage-labels.md | 13 +-- .../github/agent_workflows_pushable_test.go | 52 ++++++++++++ .../automerge_adopt_workflows_grant_test.go | 82 ++++++++++++++----- .../automerge/automerge_contributor_adopt.go | 48 ++++++----- src/pkg/github/issue_workflows_handoff.go | 20 +++++ 7 files changed, 175 insertions(+), 48 deletions(-) create mode 100644 src/pkg/github/agent_workflows_pushable_test.go diff --git a/changelog.d/fixed-adopt-in-place-workflows-grant.md b/changelog.d/fixed-adopt-in-place-workflows-grant.md index d408004d21..dec45c550e 100644 --- a/changelog.d/fixed-adopt-in-place-workflows-grant.md +++ b/changelog.d/fixed-adopt-in-place-workflows-grant.md @@ -1 +1 @@ -- Adoption no longer closes and re-opens a contributor PR whose branch lives in the base repository just because the base changed a file under `.github/workflows/`. The hive used to assume the App can never push such a base-sync and superseded the PR, which dropped its approvals and number (projectbluefin/common#1689 became #1708). Now it checks the installation: when the App installation grants `workflows: write` and a trusted- or merger-tier lane serves the repository, the PR is adopted in place on its own branch. Fork PRs are unchanged, because GitHub refuses any App workflow-file push to a fork whatever the base installation grants. A refused `update-branch` during adopted-PR recovery now logs `fork-workflow-update` or `workflow-permission` instead of the catch-all `update-branch-failed`. +- Adoption no longer closes and re-opens a contributor PR whose branch lives in the base repository just because the base changed a file under `.github/workflows/`. The rule came from #12865, written when the App had no Workflows grant (#6985). That premise has changed: an installation can grant `workflows: write` (#14664). Under the old rule the hive superseded such PRs, which dropped their approvals and number (projectbluefin/common#1689 became #1708). Now the hive keeps the PR on its own branch when the fixer that rebases adopted heads (`scanner`) can push workflow files itself: its tier is trusted or merger and the App installation grants `workflows: write`. The same check lets a DCO-red adopted head on such a branch be repaired in place instead of re-homed. Fork PRs are unchanged, because GitHub refuses any App workflow-file push to a fork whatever the base installation grants. A refused `update-branch` during adopted-PR recovery now logs `fork-workflow-update` or `workflow-permission` instead of the catch-all `update-branch-failed`. diff --git a/src/cmd/hive/merge_eligibility.go b/src/cmd/hive/merge_eligibility.go index 1546fbd547..8f785f6d90 100644 --- a/src/cmd/hive/merge_eligibility.go +++ b/src/cmd/hive/merge_eligibility.go @@ -1139,15 +1139,15 @@ func prFixAgent(pr github.PullRequest, audited string) string { func routeConflictFixAgent(agent string, cfg *config.Config) (string, string) { owner := strings.TrimSpace(agent) if owner == "" { - return "scanner", "" + return github.FallbackFixAgent, "" } - if strings.EqualFold(owner, "scanner") || cfg == nil { + if strings.EqualFold(owner, github.FallbackFixAgent) || cfg == nil { return owner, "" } base := cfg.BaseAgentName(owner) ac, ok := cfg.Agents[base] if !ok || ac.Paused || !cfg.HasAnyCadence(base) || allConfiguredCadencesPaused(cfg, base) { - return "scanner", owner + return github.FallbackFixAgent, owner } return owner, "" } diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index c7b5422829..1cae17b604 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -1230,10 +1230,12 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human- fork allows edits by maintainers (`maintainer_can_modify`, resolved with one `GET pulls/N` at adoption time). If the base changed a file under `.github/workflows/` since the PR's merge-base, a head in the base repo - stays in place only when the App installation grants `workflows: write` - and a lane whose token asks for it (trusted or merger tier) serves the - repo, so the base-sync can be pushed; a fork head never does (see - above). The hive posts ONE + stays in place only when the fixer that rebases and fixes adopted heads + (`scanner`) can push workflow files itself: its token tier (trusted or + merger) asks for the Workflows permission and the App installation + grants `workflows: write`. A fork head never does (see above). The same + check decides whether a DCO-red adopted head is repaired in place or + re-homed. The hive posts ONE `` comment, moves the PR to `stage/074-adopted-by-hive` and files it in `ci-failing.json` as its own (`"adopted": true`, `reachable_action: @@ -1356,7 +1358,8 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human- - **supersede** (`mode=supersede`) — the fork forbids maintainer edits, `in_place: false`, the base-sync of a head in the base repo carries a `.github/workflows/` change the hive cannot push (no `workflows: write` - grant, or the head...base compare failed), or the adopted head has + grant, the fixer's token cannot push workflows, or the head...base + compare failed), or the adopted head has unsigned commits (`no-dco`) that the hive cannot repair by amending the contributor branch: - pushes `hive/adopt-` in the base repo at the contributor's head SHA diff --git a/src/pkg/github/agent_workflows_pushable_test.go b/src/pkg/github/agent_workflows_pushable_test.go new file mode 100644 index 0000000000..5b6fce5f04 --- /dev/null +++ b/src/pkg/github/agent_workflows_pushable_test.go @@ -0,0 +1,52 @@ +package github + +import ( + "context" + "errors" + "testing" +) + +// AgentWorkflowsPushable asks about the one lane that will push, not about +// any capable lane: the adoption sweep keeps a same-repo PR on its own branch +// across a base workflow change only when the fallback fixer can push it +// (#12865: a contributor-tier fixer's push was refused while another lane +// held the grant). +func TestAgentWorkflowsPushable(t *testing.T) { + write := map[string]string{"workflows": "write"} + cases := []struct { + name string + appAuth bool + fixerTrusted bool + perms map[string]string + err error + want bool + }{ + {name: "grant present, fixer at contributor tier", appAuth: true, perms: write}, + {name: "grant present, fixer at trusted tier", appAuth: true, fixerTrusted: true, perms: write, want: true}, + {name: "token-authenticated client", fixerTrusted: true, perms: write}, + {name: "installation grants read only", appAuth: true, fixerTrusted: true, perms: map[string]string{"workflows": "read"}}, + {name: "installation read fails", appAuth: true, fixerTrusted: true, err: errors.New("502")}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + stubInstallation(t, tc.perms, tc.err) + c := &Client{} + if tc.appAuth { + c.appAuth = &AppAuth{} + } + // Another lane (ci-maintainer) always holds a trusted-tier + // token: the answer for the fixer must not borrow it. + c.SetWorkflowsSelfPushFunc(func(agent string) bool { + return agent == "ci-maintainer" || (agent == FallbackFixAgent && tc.fixerTrusted) + }) + c.SetWorkflowsLaneFunc(func(string, string) string { return "ci-maintainer" }) + if got := c.AgentWorkflowsPushable(context.Background(), FallbackFixAgent); got != tc.want { + t.Fatalf("AgentWorkflowsPushable(%s) = %v, want %v", FallbackFixAgent, got, tc.want) + } + }) + } + var nilClient *Client + if nilClient.AgentWorkflowsPushable(context.Background(), FallbackFixAgent) { + t.Fatal("nil client reported a pushable fixer") + } +} diff --git a/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go b/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go index be2c333136..e8fe9798f5 100644 --- a/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go +++ b/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go @@ -1,8 +1,10 @@ package automerge import ( + "bytes" "context" "errors" + "log/slog" "net/http" "strings" "sync" @@ -14,8 +16,9 @@ import ( ) // workflowsGrantTransport is the engine's client with a fixed answer to -// WorkflowsPushable: an installation that grants (or lacks) `workflows: -// write` with a lane that asks for it serving the repo. +// AgentWorkflowsPushable: the fallback fixer's tier asks for the Workflows +// permission and the installation grants it (or not). The real predicate is +// covered in pkg/github (TestAgentWorkflowsPushable). type workflowsGrantTransport struct { *hgithub.Client pushable bool @@ -24,14 +27,14 @@ type workflowsGrantTransport struct { asked []string } -func (t *workflowsGrantTransport) WorkflowsPushable(_ context.Context, repo string) bool { +func (t *workflowsGrantTransport) AgentWorkflowsPushable(_ context.Context, agent string) bool { t.mu.Lock() defer t.mu.Unlock() - t.asked = append(t.asked, repo) + t.asked = append(t.asked, agent) return t.pushable } -func (t *workflowsGrantTransport) askedRepos() []string { +func (t *workflowsGrantTransport) askedAgents() []string { t.mu.Lock() defer t.mu.Unlock() return append([]string(nil), t.asked...) @@ -49,9 +52,10 @@ func withWorkflowsGrant(t *testing.T, h *adoptHarness, pushable bool) *workflows } // A base that changed .github/workflows/ supersedes a branch of the base -// repository only when the hive cannot push workflow files there. With the -// installation's `workflows: write` grant, the base-sync lands on the PR's -// own branch, so the PR is adopted in place and keeps its number and +// repository only when the fixer that works an adopted head cannot push +// workflow files there. When its tier asks for the Workflows permission and +// the installation grants `workflows: write`, the base-sync lands on the +// PR's own branch, so the PR is adopted in place and keeps its number and // approvals (projectbluefin/common#1689 was closed and re-opened as #1708). // A compare failure still supersedes. func TestAdoptionInPlaceWorkflowGuardHonoursGrant(t *testing.T) { @@ -63,10 +67,10 @@ func TestAdoptionInPlaceWorkflowGuardHonoursGrant(t *testing.T) { wantInPlace bool wantAsked bool }{ - {name: "grant: same-repo head adopted in place", pushable: true, files: []string{".github/workflows/build.yml"}, wantInPlace: true, wantAsked: true}, - {name: "no grant: same-repo head superseded", files: []string{".github/workflows/build.yml"}, wantAsked: true}, - {name: "grant: compare error still supersedes", pushable: true, failCompare: true}, - {name: "grant: no workflow change needs no grant read", pushable: true, files: []string{"src/a.go"}, wantInPlace: true}, + {name: "fixer can push workflows: same-repo head adopted in place", pushable: true, files: []string{".github/workflows/build.yml"}, wantInPlace: true, wantAsked: true}, + {name: "fixer cannot push workflows: same-repo head superseded", files: []string{".github/workflows/build.yml"}, wantAsked: true}, + {name: "fixer can push, compare error still supersedes", pushable: true, failCompare: true}, + {name: "no workflow change needs no capability read", pushable: true, files: []string{"src/a.go"}, wantInPlace: true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -92,13 +96,13 @@ func TestAdoptionInPlaceWorkflowGuardHonoursGrant(t *testing.T) { if superseded := len(h.adopt.pulls) == 1; superseded == tc.wantInPlace { t.Fatalf("superseding PRs = %d, in-place want %v", len(h.adopt.pulls), tc.wantInPlace) } - asked := tr.askedRepos() + asked := tr.askedAgents() if (len(asked) > 0) != tc.wantAsked { - t.Fatalf("grant read for %v, want asked=%v", asked, tc.wantAsked) + t.Fatalf("capability read for %v, want asked=%v", asked, tc.wantAsked) } - for _, repo := range asked { - if repo != "acme/widget" { - t.Fatalf("grant read for %q, want acme/widget", repo) + for _, agent := range asked { + if agent != hgithub.FallbackFixAgent { + t.Fatalf("capability read for %q, want the fallback fixer %q", agent, hgithub.FallbackFixAgent) } } }) @@ -130,8 +134,8 @@ func TestAdoptionForkWorkflowGuardIgnoresGrant(t *testing.T) { if rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)); !rec.ForkRehomeRefused("aaa") { t.Fatalf("fork head not recorded as staying with its author: %+v", rec) } - if asked := tr.askedRepos(); len(asked) != 0 { - t.Fatalf("fork head read the base installation's grant: %v", asked) + if asked := tr.askedAgents(); len(asked) != 0 { + t.Fatalf("fork head read the fixer's workflows capability: %v", asked) } } @@ -163,3 +167,43 @@ func TestAdoptionRecoveryUpdateBranchSkip(t *testing.T) { t.Fatalf("plain error: %q", got) } } + +// projectbluefin/utah-packages#1173 and projectbluefin/actions#752: an +// in-place adoption of a fork head whose base changed .github/workflows/. +// The stale-red recovery's update-branch is refused (GitHub will not let an +// App push workflow files to a fork) and the log names that refusal instead +// of the catch-all update-branch-failed. +func TestAdoptedRecoveryForkWorkflowRefusalNamed(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + h.adopt.allowMaintainerEdits(7, true) + h.observe(forkPR(7, "aaa"), recoveryReason) + key := stageKey("acme", "widget", 7) + h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "aaa", AdoptReasonGrace, h.clock.at) + h.api.reset() + h.adopt.compareBase = "base1" + h.adopt.compareBehind = 18 + h.adopt.baseChecks = map[string]string{"coverage": "success"} + h.adopt.updateBranchStatus = http.StatusForbidden + h.adopt.updateBranchMessage = workflowPermissionRefusal + h.clock.advance(adoptedDwell + time.Minute) + var logs bytes.Buffer + h.e.logger = slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelWarn})) + + h.observe(forkPR(7, "aaa", adoptedLabels...), recoveryReason) + if len(h.adopt.branchUpdates) != 1 { + t.Fatalf("branch updates=%v, want one refused attempt", h.adopt.branchUpdates) + } + if len(h.adopt.pulls) != 0 || len(h.adopt.refs) != 0 { + t.Fatalf("fork code copied into the base repository: pulls=%v refs=%v", h.adopt.pulls, h.adopt.refs) + } + out := logs.String() + if !strings.Contains(out, "reason=skipped:fork-workflow-update") { + t.Fatalf("recovery log does not name the fork workflow refusal:\n%s", out) + } + if strings.Contains(out, "update-branch-failed") { + t.Fatalf("recovery log kept the catch-all reason:\n%s", out) + } +} diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index 7a16be2bff..dea0451dd1 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -2186,14 +2186,20 @@ func (c *Engine) pushAccessLost(ctx context.Context, obs stageObservation) bool // under .github/workflows/ since the PR's merge-base that the hive cannot // push into the PR's branch, so in-place adoption would be a dead end // (#12865). GitHub refuses an App push that creates or updates a workflow -// file without the `workflows` grant (#6985). On a contributor's fork no -// grant on the base repository helps: the fork is not part of the -// installation (projectbluefin/testsuite#1037). On a branch of the base -// repository the push lands when the installation grants `workflows: write` -// and a lane whose token asks for it serves the repo (#4677, #14664; -// hgithub.Client.WorkflowsPushable), so sameRepo heads supersede only when -// that grant is missing or unknown. A compare failure counts as touching: a -// superseding PR is always deliverable. +// file without the `workflows` grant (#6985). #12865 applied this to every +// head because the App had no such grant then. Its premise has changed for +// a branch of the base repository: an installation can grant `workflows: +// write` (#14664), and pr_merge_state.go fences only forks. On a +// contributor's fork no grant on the base repository helps, because the +// fork is not part of the installation (projectbluefin/testsuite#1037). So +// a sameRepo head supersedes only when the fallback fixer, which rebases +// and fixes an adopted head (hgithub.FallbackFixAgent), cannot push +// workflow files itself: its tier lacks the Workflows permission or the +// installation's grant is missing or unknown +// (hgithub.Client.AgentWorkflowsPushable). The governor's update-branch +// would need only the grant, but a conflicting head is the fixer's to push. +// A compare failure counts as touching: a superseding PR is always +// deliverable. func (c *Engine) baseSyncTouchesWorkflows(ctx context.Context, obs stageObservation, sameRepo bool) bool { file, err := c.baseSyncWorkflowFile(ctx, obs) if err != nil { @@ -2203,26 +2209,28 @@ func (c *Engine) baseSyncTouchesWorkflows(ctx context.Context, obs stageObservat if file == "" { return false } - if sameRepo && c.workflowsPushable(ctx, obs.Owner+"/"+obs.Repo) { - c.info("stage labels: base-sync touches .github/workflows/ and the installation grants workflows: write; keeping the PR's own branch", "repo", obs.Display, "pr", obs.Number, "file", file) + if sameRepo && c.fixerPushesWorkflows(ctx) { + c.info("stage labels: base-sync touches .github/workflows/ and the fixer can push it; keeping the PR's own branch", "repo", obs.Display, "pr", obs.Number, "file", file, "fixer", hgithub.FallbackFixAgent) return false } c.info("stage labels: base-sync touches .github/workflows/ which the App cannot push; superseding", "repo", obs.Display, "pr", obs.Number, "file", file, "same_repo", sameRepo) return true } -// workflowsPushableTransport is the optional transport hook that reports -// whether the hive can push .github/workflows/** in a repository: the App -// installation grants `workflows: write` and a lane whose token asks for it -// serves the repo (hgithub.Client.WorkflowsPushable). Unknown reads false. -type workflowsPushableTransport interface { - WorkflowsPushable(ctx context.Context, repo string) bool +// agentWorkflowsPushableTransport is the optional transport hook that +// reports whether an agent's own pushes may touch .github/workflows/**: its +// tier asks for the Workflows permission and the App installation grants +// `workflows: write` (hgithub.Client.AgentWorkflowsPushable). Unknown reads +// false. +type agentWorkflowsPushableTransport interface { + AgentWorkflowsPushable(ctx context.Context, agent string) bool } -// workflowsPushable reports the transport's answer, false when it has none. -func (c *Engine) workflowsPushable(ctx context.Context, repo string) bool { - t, ok := c.transport.(workflowsPushableTransport) - return ok && t.WorkflowsPushable(ctx, repo) +// fixerPushesWorkflows reports whether the fallback fixer that works an +// adopted head can push workflow files, false when the transport cannot say. +func (c *Engine) fixerPushesWorkflows(ctx context.Context) bool { + t, ok := c.transport.(agentWorkflowsPushableTransport) + return ok && t.AgentWorkflowsPushable(ctx, hgithub.FallbackFixAgent) } // baseSyncWorkflowFile returns the first file under .github/workflows/ that diff --git a/src/pkg/github/issue_workflows_handoff.go b/src/pkg/github/issue_workflows_handoff.go index d2de519c7c..82710d6b0f 100644 --- a/src/pkg/github/issue_workflows_handoff.go +++ b/src/pkg/github/issue_workflows_handoff.go @@ -213,6 +213,26 @@ func (c *Client) WorkflowsPushable(ctx context.Context, repo string) bool { return known && granted } +// FallbackFixAgent is the lane that rebases and fixes CI on a PR no lane +// owns by branch name: an adopted, trusted-author or operator-requested head +// (cmd/hive routeConflictFixAgent). The adoption sweep asks whether this +// lane's own pushes may touch workflow files before it keeps an adopted PR +// on its own branch across a base workflow change. +const FallbackFixAgent = "scanner" + +// AgentWorkflowsPushable reports whether agent's own pushes may touch +// .github/workflows/**: its token tier asks for the Workflows permission +// (SetWorkflowsSelfPushFunc: trusted or merger) and the App installation +// grants `workflows: write`. A token-authenticated client, an unknown +// grant or an agent the lookup does not know reads false. +func (c *Client) AgentWorkflowsPushable(ctx context.Context, agent string) bool { + if !c.workflowsSelfPushFor(agent) { + return false + } + granted, known := c.installationGrantsWorkflows(ctx) + return known && granted +} + // CapabilityAvailable is the blocker ledger's check (blocker.Ledger // SetCapabilityCheck): a workflows-permission blocker on repo#number is // refuted when WorkflowsPushable holds and the item is not a fork PR. A fork