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..dec45c550e --- /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 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/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..1cae17b604 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -1228,7 +1228,14 @@ 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 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: @@ -1349,8 +1356,12 @@ 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, 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 (the fork's commits, authorship and `Signed-off-by` preserved byte for byte — "sign-off survives the squash"); 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 new file mode 100644 index 0000000000..e8fe9798f5 --- /dev/null +++ b/src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go @@ -0,0 +1,209 @@ +package automerge + +import ( + "bytes" + "context" + "errors" + "log/slog" + "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 +// 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 + + mu sync.Mutex + asked []string +} + +func (t *workflowsGrantTransport) AgentWorkflowsPushable(_ context.Context, agent string) bool { + t.mu.Lock() + defer t.mu.Unlock() + t.asked = append(t.asked, agent) + return t.pushable +} + +func (t *workflowsGrantTransport) askedAgents() []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 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) { + cases := []struct { + name string + pushable bool + files []string + failCompare bool + wantInPlace bool + wantAsked bool + }{ + {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) { + 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.askedAgents() + if (len(asked) > 0) != tc.wantAsked { + t.Fatalf("capability read for %v, want asked=%v", asked, tc.wantAsked) + } + for _, agent := range asked { + if agent != hgithub.FallbackFixAgent { + t.Fatalf("capability read for %q, want the fallback fixer %q", agent, hgithub.FallbackFixAgent) + } + } + }) + } +} + +// 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.askedAgents(); len(asked) != 0 { + t.Fatalf("fork head read the fixer's workflows capability: %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) + } +} + +// 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_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..dea0451dd1 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,54 @@ 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). #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 { 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.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 +} + +// 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 +} + +// 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