diff --git a/changelog.d/fixed-workflows-blocker-verified.md b/changelog.d/fixed-workflows-blocker-verified.md new file mode 100644 index 0000000000..aa3401ca8a --- /dev/null +++ b/changelog.d/fixed-workflows-blocker-verified.md @@ -0,0 +1 @@ +- An agent's "the App cannot push `.github/workflows/`" is checked before it parks anything. On projectbluefin/testsuite#1059 and #1073, scanner (ISSUES_PRS_MERGE, whose token asks for the Workflows permission) parked a workflow fix the same day the App pushed `e2e.yml` in #1089, and the issues showed "Blocked · App lacks workflows permission" for four days. Now the relay refuses that park when the parking lane's tier pushes workflows and the App installation grants `workflows: write` (read at most every five minutes), and hands a contributor-tier park to the workflows lane only when the installation is not known to lack the permission. The blocker ledger records a workflows blocker only when no lane can push workflow files on that repository or the item is a fork PR (a fork branch takes no workflow push from the App), and a re-check that runs with the un-park sweep releases existing ones and routes them to the workflows lane. Merger- and trusted-tier kicks now say their token can push `.github/workflows/**`. A park that quotes GitHub's own rejection of the push still stands, and that evidence is kept across later unquoted re-reports; a contributor-tier lane's quote is not treated as evidence about the App. diff --git a/src/cmd/hive/github_client_config.go b/src/cmd/hive/github_client_config.go index 082b971ca4..6d2cac5323 100644 --- a/src/cmd/hive/github_client_config.go +++ b/src/cmd/hive/github_client_config.go @@ -13,6 +13,7 @@ import ( "github.com/hivecommons/hive/pkg/attention" "github.com/hivecommons/hive/pkg/authorkind" "github.com/hivecommons/hive/pkg/beads" + itemblocker "github.com/hivecommons/hive/pkg/blocker" "github.com/hivecommons/hive/pkg/claims" "github.com/hivecommons/hive/pkg/dashboard" "github.com/hivecommons/hive/pkg/github" @@ -157,6 +158,14 @@ func (b *boot) applyGitHubClientConfigHooks(client *github.Client) { // to the lane that can, instead of the owner (issue_workflows_handoff.go). // Live predicate over the shared config, like the scope funcs above. client.SetWorkflowsLaneFunc(b.cfg.WorkflowsLaneFor) + // A lane whose own token asks for the Workflows permission is refused a + // workflow-only park when the installation grants it, and the blocker + // ledger records "App lacks workflows" only when the hive cannot push + // them (projectbluefin/testsuite#1059). The ledger is process-wide, so a + // rebuilt client re-installs its check. + client.SetWorkflowsSelfPushFunc(b.cfg.AgentPushesWorkflows) + itemblocker.Shared().SetCapabilityCheck(client.CapabilityAvailable) + itemblocker.Shared().SetProofCheck(client.WorkflowsRejectionProves) client.SetReporterConfirmationEnabledFunc(func() bool { return b.cfg.Issues.ReporterConfirmationEnabled() }) diff --git a/src/cmd/hive/main_sweeps.go b/src/cmd/hive/main_sweeps.go index 8a6097a195..5e4bda4731 100644 --- a/src/cmd/hive/main_sweeps.go +++ b/src/cmd/hive/main_sweeps.go @@ -1499,6 +1499,15 @@ func runIssueUnparkSweepIfDue(ctx context.Context, ghClient *github.Client, cfg if lastRun != nil { *lastRun = now } + // Capability blockers recorded before the hive could check them, or + // before the permission was granted, are re-checked at the same cadence + // (and on the first pass after start), so a false "App lacks workflows + // permission" clears itself (projectbluefin/testsuite#1059). + if released, err := ghClient.SweepWorkflowsBlockers(ctx); err != nil { + logger.Warn("workflows blocker re-check: labelling released items failed", "released", len(released), "error", err) + } else if len(released) > 0 { + logger.Info("workflows blocker re-check released items", "released", len(released)) + } // `/hive approve` on a PR is answered with what the merge lane will do // with it (hivecommons/hive#13087); the engine here only reads policy. approveOpts := automerge.Options{ diff --git a/src/docs/dashboard-sections/activity-monitor.md b/src/docs/dashboard-sections/activity-monitor.md index 7b662f251b..be78aa9c5f 100644 --- a/src/docs/dashboard-sections/activity-monitor.md +++ b/src/docs/dashboard-sections/activity-monitor.md @@ -382,7 +382,7 @@ A `blocked` issue with neither (a human-applied label) keeps the bare pill. This #### Blocked pill — capability gaps -**Blocked pill — capability gaps** ([#15099](https://github.com/hivecommons/hive/issues/15099)). An issue parked because the hive *cannot* act — the scanner reported "the App token cannot push `.github/workflows/*` (no `workflows` permission); needs a human" — is a blocker, not a question, so its row does not read *Needs human decision*: the pill reads **Blocked · App lacks workflows permission** and the same popover says in plain words *Hive can't do this one: the GitHub App has no `workflows` permission, so it cannot push changes to .github/workflows/\*.* followed by the ways out: *Grant the App workflows permission* (opens the App settings page; the hive re-tries on its own once it sees the permission), *Re-check permissions* (probes the installation now and releases every item blocked on it), *Prepare the patch for a person to push* (disabled with its reason until a human-authorised push identity exists), *Mark partially addressed and close*, *Keep waiting*. *Let hive decide* stays in the list but disabled with the sentence that explains why — a direction decision cannot grant a permission — instead of being greyed silently; running it anyway returns *no-effect* with the same words and never applies `approved-direction`. The loop that motivated this (un-park → sweep re-parks with the same text → "I tried let hive decide and it still needs human") is guarded: when the same needs-human reason comes back after a delegated un-park, the row is parked with a blocker and *Let hive decide* is disabled on it until the reason changes. The blocker comes from the agent's `` marker or, failing that, from the prose of the needs-human report (`pkg/blocker`), and ⚙ → Security shows the fleet view: *GitHub App permissions: missing `workflows` · N items blocked* with the settings link (`/api/status.capabilities`). +**Blocked pill — capability gaps** ([#15099](https://github.com/hivecommons/hive/issues/15099)). An issue parked because the hive *cannot* act — the scanner reported "the App token cannot push `.github/workflows/*` (no `workflows` permission); needs a human" — is a blocker, not a question, so its row does not read *Needs human decision*: the pill reads **Blocked · App lacks workflows permission** and the same popover says in plain words *Hive can't do this one: the GitHub App has no `workflows` permission, so it cannot push changes to .github/workflows/\*.* followed by the ways out: *Grant the App workflows permission* (opens the App settings page; the hive re-tries on its own once it sees the permission), *Re-check permissions* (probes the installation now and releases every item blocked on it), *Prepare the patch for a person to push* (disabled with its reason until a human-authorised push identity exists), *Mark partially addressed and close*, *Keep waiting*. *Let hive decide* stays in the list but disabled with the sentence that explains why — a direction decision cannot grant a permission — instead of being greyed silently; running it anyway returns *no-effect* with the same words and never applies `approved-direction`. The loop that motivated this (un-park → sweep re-parks with the same text → "I tried let hive decide and it still needs human") is guarded: when the same needs-human reason comes back after a delegated un-park, the row is parked with a blocker and *Let hive decide* is disabled on it until the reason changes. The blocker comes from the agent's `` marker or, failing that, from the prose of the needs-human report (`pkg/blocker`). A workflows blocker is recorded only when the hive cannot push workflow files for that item: when the App installation grants `workflows: write`, an enabled lane whose token asks for it serves the repository and the item is an issue or a same-repository PR, the report is kept as a plain reason and no pill is drawn. A fork PR keeps the blocker, because a fork branch takes no workflow-file push from the App whatever it is granted. The same check runs with the issue un-park sweep (on start, then every five minutes) on blockers already recorded: a released one gets `hive/workflows-lane`, so the issue goes to that lane once its park is answered. ⚙ → Security shows the fleet view: *GitHub App permissions: missing `workflows` · N items blocked* with the settings link (`/api/status.capabilities`). #### New row (`000-new`, the pre-stage state) diff --git a/src/docs/hive-open-issue.md b/src/docs/hive-open-issue.md index 01caea937b..0c12739bd1 100644 --- a/src/docs/hive-open-issue.md +++ b/src/docs/hive-open-issue.md @@ -233,7 +233,23 @@ repository (a lane scoped to other repositories with `repos:` is never chosen), the watcher adds `hive/workflows-lane` instead of the park label, appends a "Handed to ``" note and no owner-step marker, and the scheduler routes the issue to that lane. On `issue` the new issue is created at -`stage/010-actionable` with the label. +`stage/010-actionable` with the label. The hand-off is skipped (the park +stands) when the App installation is known to lack `workflows: write`. + +A lane that can push workflow files itself is refused the park instead. When +the parking lane runs at the trusted or merger tier (ISSUES_PRS_MERGE), whose +token asks for the Workflows permission, and the App installation grants +`workflows: write` (read at most every five minutes), the watcher parks +nothing, adds no label, and appends a "Not parked: ``'s token can push +`.github/workflows/`" note: the issue stays with that lane to push the change. +If the installation lacks the permission, or cannot be read, the park stands. It +also stands when the park quotes GitHub's own rejection of the push ("refusing to +allow a GitHub App to create or update workflow … without `workflows` +permission"): that is evidence, and the blocker it records is never released by +the scheduled re-check. The quote counts as evidence only from a lane whose own +token asks for the permission (or from a report with no `— hive:` trailer, such +as a person pasting the error); a contributor-tier lane's quote shows only that +its own token lacks it, so that blocker stays releasable. #### Withdrawing your own park diff --git a/src/docs/labels-and-control-signals/reference.md b/src/docs/labels-and-control-signals/reference.md index 1fab70ff32..667ee64f62 100644 --- a/src/docs/labels-and-control-signals/reference.md +++ b/src/docs/labels-and-control-signals/reference.md @@ -382,9 +382,9 @@ Not added when the same sweep is applying `needs-human`. **Applies to:** Issues. **Kind:** Routing. -Workflow hand-off. An ISSUES_AND_PRS lane's token is minted at the `contributor` tier, which has no Workflows permission, so it cannot push a change under `.github/workflows/`. When such a lane parks a workflow-only change (`hive-open-issue issue|comment --needs-decision` whose text says it cannot push the workflow file, with no lettered options for the owner) and an enabled lane that serves the repository runs at ISSUES_PRS_MERGE (one scoped to other repositories with `repos:` is never chosen), the relay does not park the issue: it adds this label, posts the lane's exact replacement with a "Handed to ``" note, and the scheduler sends the issue to that lane rather than back to the one that filed it. With no such lane, or when the ISSUES_PRS_MERGE lane itself could not push, the park goes to the owner as before. +Workflow hand-off. An ISSUES_AND_PRS lane's token is minted at the `contributor` tier, which has no Workflows permission, so it cannot push a change under `.github/workflows/`. When such a lane parks a workflow-only change (`hive-open-issue issue|comment --needs-decision` whose text says it cannot push the workflow file, with no lettered options for the owner) and an enabled lane that serves the repository runs at ISSUES_PRS_MERGE (one scoped to other repositories with `repos:` is never chosen), the relay does not park the issue: it adds this label, posts the lane's exact replacement with a "Handed to ``" note, and the scheduler sends the issue to that lane rather than back to the one that filed it. With no such lane, or when the App installation is known to lack `workflows: write`, the park goes to the owner as before. An ISSUES_PRS_MERGE lane's own workflow-only park is refused, with no label, when the installation grants `workflows: write`: its token can push the change. The scheduled blocker re-check also adds this label to an issue whose "App lacks workflows permission" blocker it releases. -**Who applies it:** Hive (issue-request relay). +**Who applies it:** Hive (issue-request relay, blocker re-check). **Checked by:** Scheduler kick assembly. **When:** Every kick build. diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index 3714ca3c68..99c8580dc2 100644 --- a/src/docs/scale-envelope.md +++ b/src/docs/scale-envelope.md @@ -34,7 +34,7 @@ ones to: 2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.** This holds today and is easy to assume backwards: - Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied - (`RankActionableIssues`, `src/pkg/github/client.go:5345-5355`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:5350-5360`, descending `AgeMinutes` within each tier). - PRs are sorted by **review class then oldest-first within class** (`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`; fixes → refactors/docs → tests). diff --git a/src/pkg/blocker/blocker.go b/src/pkg/blocker/blocker.go index 41998aba52..a33a42f65b 100644 --- a/src/pkg/blocker/blocker.go +++ b/src/pkg/blocker/blocker.go @@ -132,6 +132,24 @@ type Blocker struct { // "loop-guard". Source string `json:"source,omitempty"` Since time.Time `json:"since,omitempty"` + // Proven: the report quotes GitHub's own refusal of the push + // (QuotesWorkflowsRejection), so it is evidence, not a claim, and no + // capability check refutes it. + Proven bool `json:"proven,omitempty"` +} + +// workflowsRejectionRE is GitHub's refusal of a workflow-file push, as it +// reads for an App, an OAuth App or a token: "refusing to allow a GitHub App +// to create or update workflow `.github/workflows/x.yml` without `workflows` +// permission". +var workflowsRejectionRE = regexp.MustCompile("(?i)refusing to allow an? (GitHub App|OAuth App|Personal Access Token|integration)[^.\n]{0,40}? to (create or update|create|update) workflow") + +// QuotesWorkflowsRejection reports whether text quotes GitHub's own refusal +// of a workflow-file push. A lane told its token can push workflow files +// parks again with that error when a push still bounces; the quote is what +// separates that from an unverified "the App cannot push workflows". +func QuotesWorkflowsRejection(text string) bool { + return workflowsRejectionRE.MatchString(text) } const ( @@ -210,10 +228,10 @@ func Infer(prose string) (Kind, bool) { // sentence the kind was inferred from. func Detect(text string, at time.Time) (Blocker, bool) { if k, ok := ParseMarker(text); ok { - return Blocker{Kind: k, Detail: ReasonSentence(text), Source: SourceMarker, Since: at}, true + return Blocker{Kind: k, Detail: ReasonSentence(text), Source: SourceMarker, Since: at, Proven: k == CapabilityWorkflowsPermission && QuotesWorkflowsRejection(text)}, true } if k, ok := Infer(text); ok { - return Blocker{Kind: k, Detail: ReasonSentence(text), Source: SourceInferred, Since: at}, true + return Blocker{Kind: k, Detail: ReasonSentence(text), Source: SourceInferred, Since: at, Proven: k == CapabilityWorkflowsPermission && QuotesWorkflowsRejection(text)}, true } return Blocker{}, false } diff --git a/src/pkg/blocker/ledger.go b/src/pkg/blocker/ledger.go index 0e5e4d428d..e13f3416a8 100644 --- a/src/pkg/blocker/ledger.go +++ b/src/pkg/blocker/ledger.go @@ -56,6 +56,50 @@ type Ledger struct { entries map[string]Entry now func() time.Time loaded bool + // capabilityCheck refutes a capability report (SetCapabilityCheck). + capabilityCheck func(repo string, number int, k Kind) bool + // proofCheck confirms a quoted rejection proves the gap (SetProofCheck). + proofCheck func(text string) bool +} + +// SetProofCheck installs the check Observe asks before it marks a quoted +// GitHub rejection as proof (Blocker.Proven): true when the quote came from a +// reporter whose own token asks for the permission. A contributor-tier lane +// quoting GitHub's refusal shows only that its own token lacks `workflows`, +// not that the App does, so its blocker stays refutable. Nil, the default, +// trusts every quote. +func (l *Ledger) SetProofCheck(fn func(text string) bool) { + l.mu.Lock() + defer l.mu.Unlock() + l.proofCheck = fn +} + +func (l *Ledger) proofHolds(text string) bool { + l.mu.Lock() + fn := l.proofCheck + l.mu.Unlock() + return fn == nil || fn(text) +} + +// SetCapabilityCheck installs the check Observe asks before it records a +// capability blocker: true means the hive verified it holds that capability +// for that item (for workflows: the installation grants `workflows: write`, +// a lane whose token asks for it serves the repo, and the item is not a fork +// PR), so an agent's "the App cannot push workflow files" is not recorded +// as a blocker (projectbluefin/testsuite#1059). Nil, the default, records +// every report. +func (l *Ledger) SetCapabilityCheck(fn func(repo string, number int, k Kind) bool) { + l.mu.Lock() + defer l.mu.Unlock() + l.capabilityCheck = fn +} + +// capabilityHeld runs the check outside the ledger lock: it may read GitHub. +func (l *Ledger) capabilityHeld(repo string, number int, k Kind) bool { + l.mu.Lock() + fn := l.capabilityCheck + l.mu.Unlock() + return fn != nil && k.IsCapability() && fn(repo, number, k) } // NewLedger returns a ledger backed by path (loaded lazily; a missing or @@ -140,6 +184,14 @@ func (l *Ledger) Observe(repo string, number int, text string, at time.Time) (En } return l.RecordReason(repo, number, NormalizeReason(text), at), true } + if b.Proven && !l.proofHolds(text) { + b.Proven = false + } + if !b.Proven && l.capabilityHeld(repo, number, b.Kind) { + // The hive holds the capability the report says is missing: keep the + // reason for the loop guard, park nothing on it. + return l.RecordReason(repo, number, NormalizeReason(text), at), true + } return l.Record(repo, number, b, NormalizeReason(text)), true } @@ -165,6 +217,12 @@ func (l *Ledger) Record(repo string, number int, b Blocker, reason string) Entry if e.Blocker != nil && e.Blocker.Kind == b.Kind && !e.Blocker.Since.IsZero() && e.Blocker.Since.Before(b.Since) { b.Since = e.Blocker.Since } + // Evidence outlives a later unquoted re-report of the same kind, which + // lands here whenever the capability check could not refute it (an + // unreadable installation): Proven must not drop with it. + if e.Blocker != nil && e.Blocker.Kind == b.Kind && e.Blocker.Proven { + b.Proven = true + } e.Blocker = &b e.Reason = reason e.ReportedAt = observedAt @@ -278,6 +336,46 @@ func (l *Ledger) ClearKind(k Kind) int { return n } +// RecheckCapabilities lifts every capability blocker that held(repo, number, kind) +// now refutes and returns the released entries as they were. It is the +// scheduled form of the dashboard's Re-check permissions, per item rather +// than per kind, so a blocker on a repo no capable lane serves stays, and so +// does one whose report quoted GitHub's rejection (Blocker.Proven). held +// runs outside the ledger lock; a blocker that changed kind meanwhile is kept. +func (l *Ledger) RecheckCapabilities(held func(repo string, number int, k Kind) bool) []Entry { + if held == nil { + return nil + } + var refuted []Entry + for _, e := range l.Blockers() { + if e.Blocker.Kind.IsCapability() && !e.Blocker.Proven && held(e.Repo, e.Number, e.Blocker.Kind) { + refuted = append(refuted, e) + } + } + if len(refuted) == 0 { + return nil + } + l.mu.Lock() + defer l.mu.Unlock() + var released []Entry + for _, was := range refuted { + k := Key(was.Repo, was.Number) + e, ok := l.entries[k] + if !ok || e.Blocker == nil || e.Blocker.Kind != was.Blocker.Kind || !e.Blocker.Since.Equal(was.Blocker.Since) { + continue + } + e.Blocker = nil + e.Reapplied = false + e.ClearedAt = l.now() + l.entries[k] = e + released = append(released, was) + } + if len(released) > 0 { + _ = l.save() + } + return released +} + // Blockers lists every current blocker, sorted by item key. func (l *Ledger) Blockers() []Entry { l.mu.Lock() diff --git a/src/pkg/blocker/ledger_capability_check_test.go b/src/pkg/blocker/ledger_capability_check_test.go new file mode 100644 index 0000000000..a5d2820a5d --- /dev/null +++ b/src/pkg/blocker/ledger_capability_check_test.go @@ -0,0 +1,131 @@ +package blocker + +import ( + "strings" + "testing" + "time" +) + +// projectbluefin/testsuite#1059: "the hive app cannot push +// .github/workflows/" became a workflows blocker with no check, though the +// App pushed e2e.yml the same day. With a capability check installed, a +// report the hive refutes keeps its reason and parks nothing. +func TestObserveSkipsRefutedCapabilityBlocker(t *testing.T) { + report := "needs-human: the hive app cannot push `.github/workflows/e2e.yml`" + at := time.Date(2026, 11, 1, 5, 9, 13, 0, time.UTC) + + l := NewLedger("") + var asked []Kind + l.SetCapabilityCheck(func(repo string, number int, k Kind) bool { + asked = append(asked, k) + return repo == "o/held" && number != 4 + }) + if e, _ := l.Observe("o/held", 1, report, at); e.Blocks() || e.Reason == "" { + t.Fatalf("refuted report: %+v, want no blocker and the reason kept", e) + } + if e, _ := l.Observe("o/lacking", 2, report, at); !e.Blocks() || e.Blocker.Kind != CapabilityWorkflowsPermission { + t.Fatalf("unrefuted report: %+v, want a workflows blocker", e) + } + if e, _ := l.Observe("o/held", 4, report, at); !e.Blocks() { + t.Fatalf("the check is asked per item: %+v, want #4 blocked", e) + } + asked = nil + if e, _ := l.Observe("o/held", 3, "needs-human: the contributor must sign the CLA", at); !e.Blocks() || len(asked) != 0 { + t.Fatalf("human-only report: %+v asked=%v, want a blocker and no capability check", e, asked) + } + if e, _ := NewLedger("").Observe("o/held", 1, report, at); !e.Blocks() { + t.Fatalf("no check installed: %+v, want the blocker as before", e) + } +} + +func TestRecheckCapabilitiesReleasesOnlyRefuted(t *testing.T) { + l := NewLedger("") + since := time.Date(2026, 11, 1, 5, 9, 13, 0, time.UTC) + l.Record("o/a", 1, Blocker{Kind: CapabilityWorkflowsPermission, Since: since}, "r") + l.Record("o/b", 2, Blocker{Kind: CapabilityWorkflowsPermission, Since: since}, "r") + l.Record("o/a", 3, Blocker{Kind: HumanOnlyCLA, Since: since}, "cla") + released := l.RecheckCapabilities(func(repo string, number int, k Kind) bool { return repo == "o/a" }) + if len(released) != 1 || released[0].Number != 1 { + t.Fatalf("released = %+v, want o/a#1 only", released) + } + if _, ok := l.Blocking("o/a", 1); ok { + t.Fatal("o/a#1 still blocked") + } + if _, ok := l.Blocking("o/b", 2); !ok { + t.Fatal("o/b#2 released though the check did not refute it") + } + if _, ok := l.Blocking("o/a", 3); !ok { + t.Fatal("human-only blocker released") + } + // A report from before the release stays stale. + if e, _ := l.Observe("o/a", 1, "needs-human: the App cannot push workflow files", since); e.Blocks() { + t.Fatalf("stale report re-recorded: %+v", e) + } + if l.RecheckCapabilities(nil) != nil { + t.Fatal("nil check must release nothing") + } +} + +// A report quoting GitHub's own rejection of the push is evidence: no +// capability check refutes it, at record time or on the re-check. +func TestQuotedWorkflowsRejectionIsProven(t *testing.T) { + rejection := "needs-human: the push bounced: refusing to allow a GitHub App to create or update workflow `.github/workflows/e2e.yml` without `workflows` permission" + at := time.Date(2026, 11, 5, 21, 0, 0, 0, time.UTC) + l := NewLedger("") + l.SetCapabilityCheck(func(string, int, Kind) bool { return true }) + e, _ := l.Observe("o/r", 1, rejection, at) + if !e.Blocks() || !e.Blocker.Proven { + t.Fatalf("quoted rejection: %+v, want a proven workflows blocker", e) + } + if got := l.RecheckCapabilities(func(string, int, Kind) bool { return true }); len(got) != 0 { + t.Fatalf("re-check released a proven blocker: %+v", got) + } + for text, want := range map[string]bool{ + rejection: true, + "refusing to allow an OAuth App to create or update workflow `.github/workflows/ci.yml` without `workflow` scope": true, + "refusing to allow a Personal Access Token to create or update workflow `.github/workflows/ci.yml` without `workflow` scope": true, + "the hive app cannot push `.github/workflows/`": false, + } { + if got := QuotesWorkflowsRejection(text); got != want { + t.Errorf("QuotesWorkflowsRejection(%q) = %v, want %v", text, got, want) + } + } +} + +// A same-kind re-report without the quote, recorded while the check could +// not refute it, keeps the earlier evidence (#16811 review). +func TestProvenSurvivesUnquotedReReport(t *testing.T) { + at := time.Date(2026, 11, 5, 21, 0, 0, 0, time.UTC) + l := NewLedger("") + l.Observe("o/r", 1, "needs-human: refusing to allow a GitHub App to create or update workflow `.github/workflows/e2e.yml` without `workflows` permission", at) + held := false + l.SetCapabilityCheck(func(string, int, Kind) bool { return held }) + e, _ := l.Observe("o/r", 1, "needs-human: the App cannot push workflow files", at.Add(time.Hour)) + if !e.Blocks() || !e.Blocker.Proven { + t.Fatalf("re-report dropped the evidence: %+v", e) + } + held = true + if got := l.RecheckCapabilities(func(string, int, Kind) bool { return held }); len(got) != 0 { + t.Fatalf("re-check released a proven blocker after an unquoted re-report: %+v", got) + } +} + +// A quoted rejection is proof only when the proof check accepts its +// reporter: a contributor-tier lane's quote stays refutable. +func TestQuotedRejectionProofIsReporterChecked(t *testing.T) { + at := time.Date(2026, 11, 5, 21, 0, 0, 0, time.UTC) + quote := "needs-human: refusing to allow a GitHub App to create or update workflow `.github/workflows/e2e.yml` without `workflows` permission\n— hive: agent=ci-maintainer" + l := NewLedger("") + l.SetProofCheck(func(text string) bool { return !strings.Contains(text, "agent=ci-maintainer") }) + l.SetCapabilityCheck(func(string, int, Kind) bool { return false }) + if e, _ := l.Observe("o/r", 1, quote, at); !e.Blocks() || e.Blocker.Proven { + t.Fatalf("contributor quote: %+v, want an unproven blocker", e) + } + if got := l.RecheckCapabilities(func(string, int, Kind) bool { return true }); len(got) != 1 { + t.Fatalf("an unproven contributor quote must be releasable: %+v", got) + } + scanner := strings.Replace(quote, "ci-maintainer", "scanner", 1) + if e, _ := l.Observe("o/r", 2, scanner, at); !e.Blocker.Proven { + t.Fatalf("merger-tier quote: %+v, want proven", e) + } +} diff --git a/src/pkg/config/workflows_lane.go b/src/pkg/config/workflows_lane.go index 576a38115b..36f0cef7ef 100644 --- a/src/pkg/config/workflows_lane.go +++ b/src/pkg/config/workflows_lane.go @@ -46,6 +46,13 @@ func modePushesWorkflows(mode string) bool { return false } +// AgentPushesWorkflows reports whether agent's scoped token asks for the +// Workflows permission (trusted/merger tier), so its own push may touch +// .github/workflows/** when the installation grants it. +func (c *Config) AgentPushesWorkflows(agent string) bool { + return c != nil && modePushesWorkflows(c.AgentEffectiveMode(agent)) +} + // WorkflowsCapableLane names the enabled lane whose token can push // .github/workflows/** and serves repo, the first by name; replicas are // skipped because they share their source agent's lane. A lane scoped to diff --git a/src/pkg/config/workflows_lane_test.go b/src/pkg/config/workflows_lane_test.go index 84f7eaaa81..ba396b621f 100644 --- a/src/pkg/config/workflows_lane_test.go +++ b/src/pkg/config/workflows_lane_test.go @@ -75,3 +75,19 @@ func TestWorkflowsLaneFor(t *testing.T) { }) } } + +func TestAgentPushesWorkflows(t *testing.T) { + cfg := &Config{AgentRuntimeConfig: AgentRuntimeConfig{Agents: map[string]AgentConfig{ + "scanner": {Mode: "ISSUES_PRS_MERGE", Enabled: true}, + "scanner-2": {Mode: "ISSUES_PRS_MERGE", Enabled: true, ReplicaOf: "scanner"}, + "ci-maintainer": {Mode: "ISSUES_AND_PRS", Enabled: true}, + }}} + for agent, want := range map[string]bool{"scanner": true, "scanner-2": true, "ci-maintainer": false, "unknown": false, "": false} { + if got := cfg.AgentPushesWorkflows(agent); got != want { + t.Errorf("AgentPushesWorkflows(%q) = %v, want %v", agent, got, want) + } + } + if (*Config)(nil).AgentPushesWorkflows("scanner") { + t.Error("nil config must answer false") + } +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 8a87f08e12..c6e472855f 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -135,6 +135,11 @@ type Client struct { // (SetWorkflowsLaneFunc, issue_workflows_handoff.go). workflowsLaneMu sync.RWMutex workflowsLane func(agent, repo string) string + // workflowsSelfPush reports whether an agent's own token asks for the + // Workflows permission (SetWorkflowsSelfPushFunc); workflowsGrant caches + // the installation's answer (installationGrantsWorkflows). + workflowsSelfPush func(agent string) bool + workflowsGrant workflowsGrantCache // reporterConfirmationEnabled restores the legacy close gate for all // human-filed bug-family issues when configured. Nil/false means only // per-issue opt-in markers activate the gate. diff --git a/src/pkg/github/issue_owner_step_test.go b/src/pkg/github/issue_owner_step_test.go index 6d28ca1d1d..7a56c6606e 100644 --- a/src/pkg/github/issue_owner_step_test.go +++ b/src/pkg/github/issue_owner_step_test.go @@ -241,11 +241,11 @@ func TestIssueOwnerStepNeverDowngradedByDecisionPolicy(t *testing.T) { c.SetAgentIssueDecisionPolicy("resolve") step := testReleaseStep() req := IssueRequest{Repo: "o/r", Title: "Release needed", Body: "Cut the release.", Agent: "scanner", NeedsDecision: true, OwnerStep: step} - if d := c.issueDecisionDowngradeForCreate(req, req.Body); d.Active { + if d := c.issueDecisionDowngradeForCreate(context.Background(), req, req.Body); d.Active { t.Fatalf("create with owner step must park under resolve: %+v", d) } req.Kind, req.Number = "comment", 5 - if d := c.issueDecisionDowngradeForComment(req, req.Body); d.Active { + if d := c.issueDecisionDowngradeForComment(context.Background(), req, req.Body); d.Active { t.Fatalf("comment with owner step must park under resolve: %+v", d) } // Without the owner step the same request is a plain engineering @@ -254,7 +254,7 @@ func TestIssueOwnerStepNeverDowngradedByDecisionPolicy(t *testing.T) { // classifier fails closed and an unclassified park stays a human decision. req.OwnerStep = nil req.Body = "Should the release notes live top-level or under docs/? Option A or option B." - if d := c.issueDecisionDowngradeForComment(req, req.Body); !d.Active { + if d := c.issueDecisionDowngradeForComment(context.Background(), req, req.Body); !d.Active { t.Fatal("plain engineering question must still be downgraded under resolve") } } diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index 8045d7fa24..bd9b34fdcc 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -738,7 +738,7 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f // `resolve` a non-risk Hive engineering question is not parked, // the comment records the decision and the owner-step marker is // withheld so the unpark menu does not offer `/hive done`. - issueDecisionDowngrade = c.issueDecisionDowngradeForComment(req, body) + issueDecisionDowngrade = c.issueDecisionDowngradeForComment(ctx, req, body) if !issueDecisionDowngrade.Active && len(req.BlockedBy) == 0 { if n := c.askFoldTarget(ctx, req.Repo, req.Number, body); n > 0 { req.BlockedBy, askFoldedInto = []int{n}, n @@ -775,7 +775,7 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f } if err == nil && (withdraw == nil || !withdraw.posted) { commentBody := issueOwnerStepBody(body, req.NeedsDecision && !issueDecisionDowngrade.Active, req.OwnerStep) - if issueDecisionDowngrade.Lane != "" { + if issueDecisionDowngrade.workflows() { commentBody = appendWorkflowsHandoff(commentBody, issueDecisionDowngrade) } else if issueDecisionDowngrade.Active { commentBody = appendHiveDecisionTaken(commentBody, issueDecisionDowngrade.Question) @@ -927,7 +927,7 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f } default: // "issue" var res CreateIssueResult - createLabels, createBody, d := c.issueCreateLabels(req, body) + createLabels, createBody, d := c.issueCreateLabels(ctx, req, body) if d.Active { issueDecisionDowngrade = d } @@ -1096,6 +1096,9 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f if issueDecisionDowngrade.Lane != "" { c.recordWriteAudit(AuditActionAgentIssueDecisionResolved, meta, WriteTarget{Repo: req.Repo, Number: resp.Number}, "policy", "workflows-handoff", "lane", issueDecisionDowngrade.Lane) + } else if issueDecisionDowngrade.SelfPush { + c.recordWriteAudit(AuditActionAgentIssueDecisionResolved, meta, WriteTarget{Repo: req.Repo, Number: resp.Number}, + "policy", "workflows-self-push", "lane", req.Agent) } else if issueDecisionDowngrade.Active { c.recordWriteAudit(AuditActionAgentIssueDecisionResolved, meta, WriteTarget{Repo: req.Repo, Number: resp.Number}, "policy", decisionpolicy.PolicyResolve, "question", issueDecisionDowngrade.Question) @@ -1117,16 +1120,24 @@ type issueDecisionDowngrade struct { // Lane is set when the park is a workflow-only change handed to the // lane that can push it (workflowsHandoff), not a decision the hive took. Lane string + // SelfPush is set when the parking lane's own token can push the + // workflow change and the installation grants it (workflowsHandoff): + // the park is refused and the issue stays with that lane. + SelfPush bool } +// workflows reports a workflow-only park the relay did not park: handed to +// another lane, or refused because the lane can push it itself. +func (d issueDecisionDowngrade) workflows() bool { return d.Lane != "" || d.SelfPush } + // A typed owner step (e.g. a release) proposes an action only a person // with owner credentials can take, so it is owner-only by construction and // never downgraded to a hive-taken decision, whatever the policy. -func (c *Client) issueDecisionDowngradeForCreate(req IssueRequest, body string) issueDecisionDowngrade { +func (c *Client) issueDecisionDowngradeForCreate(ctx context.Context, req IssueRequest, body string) issueDecisionDowngrade { if !req.NeedsDecision || req.OwnerStep != nil || !hiveIssueRequestActor(req.Agent, req.Labels) { return issueDecisionDowngrade{} } - if d := c.workflowsHandoff(req.Agent, req.Repo, req.Title, body); d.Active { + if d := c.workflowsHandoff(ctx, req.Agent, req.Repo, req.Title, body); d.Active { return d } return c.issueDecisionDowngrade(req.Title, body) @@ -1135,11 +1146,11 @@ func (c *Client) issueDecisionDowngradeForCreate(req IssueRequest, body string) // issueDecisionDowngradeForComment classifies a `comment --needs-decision` // park on an existing issue. A comment always carries its own text, so the // classification reads the comment body rather than the issue. -func (c *Client) issueDecisionDowngradeForComment(req IssueRequest, body string) issueDecisionDowngrade { +func (c *Client) issueDecisionDowngradeForComment(ctx context.Context, req IssueRequest, body string) issueDecisionDowngrade { if !req.NeedsDecision || req.OwnerStep != nil || !hiveIssueRequestActor(req.Agent, req.Labels) { return issueDecisionDowngrade{} } - if d := c.workflowsHandoff(req.Agent, req.Repo, "", body); d.Active { + if d := c.workflowsHandoff(ctx, req.Agent, req.Repo, "", body); d.Active { return d } return c.issueDecisionDowngrade("", body) @@ -1230,16 +1241,18 @@ func hiveDecisionDowngradeNote(question string) string { // the hive takes the engineering decision itself — stage/010-actionable in // its place and the decision recorded in the body. The labels travel in // the create call; the stage engine owns them from the first sweep on. -func (c *Client) issueCreateLabels(req IssueRequest, body string) (labels []string, createBody string, d issueDecisionDowngrade) { +func (c *Client) issueCreateLabels(ctx context.Context, req IssueRequest, body string) (labels []string, createBody string, d issueDecisionDowngrade) { labels, createBody = req.Labels, body if !req.NeedsDecision { return labels, createBody, d } - if d = c.issueDecisionDowngradeForCreate(req, createBody); d.Active { + if d = c.issueDecisionDowngradeForCreate(ctx, req, createBody); d.Active { labels = removeDecisionParkLabels(labels, c.needsDecisionLabel()) labels = appendLabelIfMissing(labels, stagelabels.Actionable.Label()) - if d.Lane != "" { - labels = appendLabelIfMissing(labels, WorkflowsLaneLabel) + if d.workflows() { + if d.Lane != "" { + labels = appendLabelIfMissing(labels, WorkflowsLaneLabel) + } createBody = appendWorkflowsHandoff(createBody, d) } else { createBody = appendHiveDecisionTaken(createBody, d.Question) diff --git a/src/pkg/github/issue_workflows_handoff.go b/src/pkg/github/issue_workflows_handoff.go index 1ea8266e3f..d2de519c7c 100644 --- a/src/pkg/github/issue_workflows_handoff.go +++ b/src/pkg/github/issue_workflows_handoff.go @@ -1,10 +1,14 @@ package github import ( + "context" "regexp" "strings" + "sync" + "time" itemblocker "github.com/hivecommons/hive/pkg/blocker" + "github.com/hivecommons/hive/pkg/writeback" ) // WorkflowsLaneLabel routes an issue to the lane that can push @@ -28,6 +32,28 @@ func (c *Client) SetWorkflowsLaneFunc(fn func(agent, repo string) string) { c.workflowsLane = fn } +// SetWorkflowsSelfPushFunc installs the fleet lookup (config.AgentPushesWorkflows) +// that says whether an agent's own scoped token asks for the Workflows +// permission. Nil, or never set, treats every lane as unable to push them. +func (c *Client) SetWorkflowsSelfPushFunc(fn func(agent string) bool) { + if c == nil { + return + } + c.workflowsLaneMu.Lock() + defer c.workflowsLaneMu.Unlock() + c.workflowsSelfPush = fn +} + +func (c *Client) workflowsSelfPushFor(agent string) bool { + if c == nil || strings.TrimSpace(agent) == "" { + return false + } + c.workflowsLaneMu.RLock() + fn := c.workflowsSelfPush + c.workflowsLaneMu.RUnlock() + return fn != nil && fn(strings.TrimSpace(agent)) +} + func (c *Client) workflowsLaneFor(agent, repo string) string { if c == nil { return "" @@ -64,22 +90,51 @@ func parkIsWorkflowsOnly(text string) bool { return strings.Contains(strings.ToLower(text), ".github/workflows") && workflowsPermissionRE.MatchString(text) } -// workflowsHandoff turns a lane's workflow-only park into a hand-off to the -// lane that can push it: the issue is not parked, it carries -// WorkflowsLaneLabel, and the posted text says where it went. Inactive when -// the requester can push workflows itself, when no lane can (the owner still -// applies it), or when the text is not a workflow-only park. -func (c *Client) workflowsHandoff(agent, repo, title, body string) issueDecisionDowngrade { +// workflowsHandoff checks a lane's workflow-only park against what the hive +// can actually push. A lane whose own token asks for the Workflows permission, +// on an installation that grants it, is refused the park and keeps the issue +// (projectbluefin/testsuite#1059: scanner, at the merger tier, parked "the +// hive app cannot push .github/workflows/" the same day the App pushed +// e2e.yml in #1089). Any other lane hands it to the lane that can push it: +// the issue is not parked, it carries WorkflowsLaneLabel, and the posted text +// says where it went. Inactive (the park stands) when the text is not a +// workflow-only park, when no lane can push it, when the installation is +// known to lack `workflows: write`, or when a lane that pushes workflow files +// quotes GitHub's rejection of its push. +func (c *Client) workflowsHandoff(ctx context.Context, agent, repo, title, body string) issueDecisionDowngrade { if !parkIsWorkflowsOnly(title + "\n" + body) { return issueDecisionDowngrade{} } + if c.workflowsSelfPushFor(agent) { + // A park quoting GitHub's rejection is what the note below asks + // for when a push still bounces: evidence, so it stands. + if itemblocker.QuotesWorkflowsRejection(title + "\n" + body) { + return issueDecisionDowngrade{} + } + if granted, known := c.installationGrantsWorkflows(ctx); known && granted { + return issueDecisionDowngrade{Active: true, SelfPush: true, Note: workflowsSelfPushNote(agent)} + } + return issueDecisionDowngrade{} + } lane := c.workflowsLaneFor(agent, repo) if lane == "" { return issueDecisionDowngrade{} } + if granted, known := c.installationGrantsWorkflows(ctx); known && !granted { + return issueDecisionDowngrade{} + } return issueDecisionDowngrade{Active: true, Lane: lane, Note: workflowsHandoffNote(agent, lane)} } +func workflowsSelfPushNote(agent string) string { + who := "this lane" + if a := strings.TrimSpace(agent); a != "" { + who = "`" + a + "`" + } + return "Not parked: " + who + "'s token can push `.github/workflows/`. Its tier asks for the Workflows permission and the App installation grants `workflows: write`, so " + + who + " pushes the change. If a push is still rejected, park again quoting GitHub's error. Owner may override with `/hive hold` or `needs-human`." +} + func workflowsHandoffNote(agent, lane string) string { from := "this lane" if a := strings.TrimSpace(agent); a != "" { @@ -92,7 +147,11 @@ func workflowsHandoffNote(agent, lane string) string { func appendWorkflowsHandoff(body string, d issueDecisionDowngrade) string { body = strings.TrimRight(body, "\n") - if strings.Contains(body, "Handed to `"+d.Lane+"`:") { + seen := d.Note + if d.Lane != "" { + seen = "Handed to `" + d.Lane + "`:" + } + if strings.Contains(body, seen) { return body } if body == "" { @@ -100,3 +159,136 @@ func appendWorkflowsHandoff(body string, d issueDecisionDowngrade) string { } return body + "\n\n" + d.Note } + +// workflowsGrantTTL is how long the installation's `workflows` grant is +// trusted before it is read again: a grant or revocation shows within a few +// minutes, and a burst of parks costs one GitHub call. +const workflowsGrantTTL = 5 * time.Minute + +type workflowsGrantCache struct { + mu sync.Mutex + at time.Time + granted bool +} + +// installationProbe reads the App installation's granted permissions; tests +// swap it. +var installationProbe = func(ctx context.Context, auth *AppAuth) (*InstallationInfo, error) { + return auth.VerifyInstallation(ctx) +} + +// installationGrantsWorkflows reports whether the App installation grants +// `workflows: write`. known is false for a token-authenticated client or a +// failed read, so callers keep their previous behaviour. +func (c *Client) installationGrantsWorkflows(ctx context.Context) (granted, known bool) { + if c == nil || c.appAuth == nil { + return false, false + } + g := &c.workflowsGrant + g.mu.Lock() + defer g.mu.Unlock() + if !g.at.IsZero() && time.Since(g.at) < workflowsGrantTTL { + return g.granted, true + } + if ctx == nil { + ctx = context.Background() + } + info, err := installationProbe(ctx, c.appAuth) + if err != nil { + return false, false + } + g.at, g.granted = time.Now(), info.Granted(itemblocker.CapabilityWorkflowsPermission.Permission(), "write") + return g.granted, true +} + +// WorkflowsPushable reports whether the hive can push .github/workflows/** +// in repo: a lane whose token asks for the Workflows permission serves it +// (an empty agent asks the lane lookup for any capable lane) and the +// installation grants `workflows: write`. Unknown is false. +func (c *Client) WorkflowsPushable(ctx context.Context, repo string) bool { + if c.workflowsLaneFor("", repo) == "" { + 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 +// branch takes no workflow-file push from the App whatever the installation +// grants (projectbluefin/testsuite#1037: the fixer's pushes were refused +// three times), so that blocker stands. Other kinds are never refuted here. +// The ledger keys webhook reports by the configured repo name, which may be +// short, so the lookups get owner/repo. +func (c *Client) CapabilityAvailable(repo string, number int, k itemblocker.Kind) bool { + if c == nil || k != itemblocker.CapabilityWorkflowsPermission { + return false + } + owner, name := c.splitRepo(repo) + ctx := context.Background() + if !c.WorkflowsPushable(ctx, owner+"/"+name) { + return false + } + fork, known := c.forkPR(ctx, owner, name, number) + return known && !fork +} + +// WorkflowsRejectionProves is the blocker ledger's proof check +// (blocker.Ledger SetProofCheck): a quoted GitHub rejection of a workflow +// push proves the App lacks `workflows` only when the reporting lane's own +// token asks for it. The reporter is the text's `— hive: agent=` trailer; a +// report without one (a person pasting the error) is trusted. +func (c *Client) WorkflowsRejectionProves(text string) bool { + agent := trailerAgent(text) + return agent == "" || c.workflowsSelfPushFor(agent) +} + +// forkPR reports whether owner/name#number is a pull request whose head is +// in another repository (or a deleted one). known is false when GitHub +// cannot be read. +func (c *Client) forkPR(ctx context.Context, owner, name string, number int) (fork, known bool) { + issue, _, err := c.client.Issues.Get(ctx, owner, name, number) + if err != nil { + return false, false + } + if !issue.IsPullRequest() { + return false, true + } + pr, _, err := c.client.PullRequests.Get(ctx, owner, name, number) + if err != nil { + return false, false + } + head := pr.GetHead().GetRepo().GetFullName() + return head == "" || !strings.EqualFold(head, owner+"/"+name), true +} + +// SweepWorkflowsBlockers re-checks the blocker ledger's capability blockers +// against what the hive can push (CapabilityAvailable) and lifts the ones it +// refutes. Each released workflows blocker gets WorkflowsLaneLabel, so once +// its park is answered the issue goes to the lane that can push the change, +// not back to the lane that parked it. The park label itself stays: a person +// still answers it, and "Let hive decide" is no longer disabled by a blocker. +// It returns the released entries. +func (c *Client) SweepWorkflowsBlockers(ctx context.Context) ([]itemblocker.Entry, error) { + if c == nil { + return nil, nil + } + released := itemblocker.Shared().RecheckCapabilities(c.CapabilityAvailable) + var firstErr error + for _, e := range released { + if e.Blocker.Kind != itemblocker.CapabilityWorkflowsPermission { + continue + } + // An unsolicited routing label, gated like the stage engine's own + // labels; a refusal leaves the blocker released and the label off. + owner, name := c.splitRepo(e.Repo) + if d := c.WritebackAllowed(owner+"/"+name, writeback.Item{Number: e.Number}, writeback.KindStageLabel, WorkflowsLaneLabel); !d.Allowed { + continue + } + if err := c.AddLabels(ctx, e.Repo, e.Number, []string{WorkflowsLaneLabel}); err != nil && firstErr == nil { // writeback:gated + firstErr = err + } + } + return released, firstErr +} diff --git a/src/pkg/github/issue_workflows_self_push_test.go b/src/pkg/github/issue_workflows_self_push_test.go new file mode 100644 index 0000000000..36e048326c --- /dev/null +++ b/src/pkg/github/issue_workflows_self_push_test.go @@ -0,0 +1,280 @@ +package github + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + itemblocker "github.com/hivecommons/hive/pkg/blocker" +) + +// projectbluefin/testsuite#1059, scanner (ISSUES_PRS_MERGE, merger tier) on +// 2026-11-01 05:09Z. The App pushed .github/workflows/e2e.yml in #1089 the +// same day. +const selfPush1059 = "## Workflow change needed (the hive app cannot push `.github/workflows/`)\n\n" + + "A test-side PR on `scanner/fix-1059` fixes most of the 16 failures. One related cause is in `e2e.yml`, and the hive app can't push it.\n\n" + + "**Proposed diff** (`.github/workflows/e2e.yml`, \"Install gnome-ponytail-daemon\" step): run the daemon under `systemd-run --user`." + +// stubInstallation swaps the installation read and counts calls. +func stubInstallation(t *testing.T, perms map[string]string, err error) *int { + t.Helper() + calls := 0 + prev := installationProbe + installationProbe = func(context.Context, *AppAuth) (*InstallationInfo, error) { + calls++ + if err != nil { + return nil, err + } + return &InstallationInfo{Permissions: perms}, nil + } + t.Cleanup(func() { installationProbe = prev }) + return &calls +} + +// fleet installs the two lookups the relay asks: scanner is the merger-tier +// lane, and it serves the repo only when lane is true. +func fleet(appAuth, lane bool) func(*Client) { + return func(c *Client) { + if appAuth { + c.appAuth = &AppAuth{} + } + c.SetWorkflowsSelfPushFunc(func(agent string) bool { return agent == "scanner" }) + c.SetWorkflowsLaneFunc(func(agent, repo string) string { + if !lane || agent == "scanner" { + return "" + } + return "scanner" + }) + } +} + +// agent tier × installation grant × capable lane: a merger-tier lane is +// refused the park when the installation grants `workflows: write`; a +// contributor-tier lane hands it to the capable lane unless the installation +// is known to lack the permission; everything else parks as before. +func TestWorkflowsParkCheckedAgainstTierAndInstallation(t *testing.T) { + const ( + self = "comment" + handoff = "label:" + WorkflowsLaneLabel + ",comment" + park = "label:owner-call,comment" + ) + write := map[string]string{"workflows": "write"} + read := map[string]string{"workflows": "read"} + for _, tc := range []struct { + name string + agent string + appAuth bool + lane bool + perms map[string]string + err error + body string + want string + }{ + {name: "merger tier, installation grants: back to the agent", agent: "scanner", appAuth: true, lane: true, perms: write, want: self}, + {name: "merger tier, no other lane needed", agent: "scanner", appAuth: true, lane: false, perms: write, want: self}, + {name: "merger tier, installation has read only: parks", agent: "scanner", appAuth: true, lane: true, perms: read, want: park}, + {name: "merger tier, installation read fails: parks", agent: "scanner", appAuth: true, lane: true, err: errors.New("502"), want: park}, + {name: "merger tier, token-authenticated client: parks", agent: "scanner", appAuth: false, lane: true, perms: write, want: park}, + {name: "merger tier quoting GitHub's rejection: parks", agent: "scanner", appAuth: true, lane: true, perms: write, body: selfPush1059 + "\n\nThe push bounced: `! [remote rejected] scanner/fix-1059 (refusing to allow a GitHub App to create or update workflow `.github/workflows/e2e.yml` without `workflows` permission)`", want: park}, + {name: "contributor tier, installation grants: hand-off", agent: "ci-maintainer", appAuth: true, lane: true, perms: write, want: handoff}, + {name: "contributor tier, installation lacks it: parks", agent: "ci-maintainer", appAuth: true, lane: true, perms: read, want: park}, + {name: "contributor tier, installation unknown: hand-off as before", agent: "ci-maintainer", appAuth: true, lane: true, err: errors.New("502"), want: handoff}, + {name: "contributor tier, no capable lane: parks", agent: "ci-maintainer", appAuth: true, lane: false, perms: write, want: park}, + } { + t.Run(tc.name, func(t *testing.T) { + stubInstallation(t, tc.perms, tc.err) + body := tc.body + if body == "" { + body = selfPush1059 + } + writes, bodies, resp := runNeedsDecisionCommentCapture(t, needsDecisionFilter{label: "owner-call"}, IssueRequest{ + Kind: "comment", Repo: "o/r", Number: 1059, Agent: tc.agent, NeedsDecision: true, Body: body, + }, false, fleet(tc.appAuth, tc.lane)) + if got := strings.Join(writes, ","); got != tc.want { + t.Fatalf("writes = %q, want %q", got, tc.want) + } + if !resp.OK || len(bodies) != 1 { + t.Fatalf("result = %+v bodies = %v", resp, bodies) + } + selfNote := strings.Contains(bodies[0], "Not parked: `scanner`'s token can push") + if selfNote != (tc.want == self) { + t.Fatalf("self-push note present = %v, want %v:\n%s", selfNote, tc.want == self, bodies[0]) + } + if tc.want != park && strings.Contains(bodies[0], unparkOwnerStepMarker) { + t.Fatalf("an unparked comment must not carry the owner-step marker:\n%s", bodies[0]) + } + }) + } +} + +// A burst of parks costs one installation read. +func TestInstallationGrantIsCached(t *testing.T) { + calls := stubInstallation(t, map[string]string{"workflows": "write"}, nil) + c := &Client{appAuth: &AppAuth{}} + for i := 0; i < 3; i++ { + if granted, known := c.installationGrantsWorkflows(context.Background()); !granted || !known { + t.Fatalf("granted=%v known=%v", granted, known) + } + } + if *calls != 1 { + t.Fatalf("installation reads = %d, want 1", *calls) + } + c.workflowsGrant.at = time.Now().Add(-workflowsGrantTTL - time.Second) + c.installationGrantsWorkflows(context.Background()) + if *calls != 2 { + t.Fatalf("installation reads after the TTL = %d, want 2", *calls) + } +} + +// The scheduled pass lifts a recorded workflows blocker once the hive can +// push workflow files on that repo, and routes the item to the capable lane. +// A repo no capable lane serves keeps its blocker. +func TestSweepWorkflowsBlockersReleasesGrantedBlockers(t *testing.T) { + stubInstallation(t, map[string]string{"workflows": "write"}, nil) + l := itemblocker.NewLedger("") + prev := itemblocker.SetShared(l) + t.Cleanup(func() { itemblocker.SetShared(prev) }) + since := time.Date(2026, 11, 1, 5, 9, 13, 0, time.UTC) + l.Record("o/testsuite", 1059, itemblocker.Blocker{Kind: itemblocker.CapabilityWorkflowsPermission, Since: since}, "the hive app cannot push .github/workflows/") + l.Record("o/other", 7, itemblocker.Blocker{Kind: itemblocker.CapabilityWorkflowsPermission, Since: since}, "the hive app cannot push .github/workflows/") + l.Record("o/testsuite", 1073, itemblocker.Blocker{Kind: itemblocker.HumanOnlyCLA, Since: since}, "sign the CLA") + l.Record("o/testsuite", 1080, itemblocker.Blocker{Kind: itemblocker.CapabilityBranchProtection, Since: since}, "cannot edit the ruleset") + // testsuite#1037's shape: a fork PR, whose branch takes no workflow push + // from the App whatever the grant. + l.Record("o/testsuite", 1037, itemblocker.Blocker{Kind: itemblocker.CapabilityWorkflowsPermission, Since: since}, "the fixer's push was refused") + // A same-repo PR is pushable like an issue. + l.Record("o/testsuite", 1089, itemblocker.Blocker{Kind: itemblocker.CapabilityWorkflowsPermission, Since: since}, "the hive app cannot push .github/workflows/") + + var mu sync.Mutex + var labelled []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/repos/o/testsuite/issues/1059": + _, _ = w.Write([]byte(`{"number":1059}`)) + return + case "/repos/o/testsuite/issues/1037", "/repos/o/testsuite/issues/1089": + _, _ = w.Write([]byte(`{"number":1,"pull_request":{"url":"x"}}`)) + return + case "/repos/o/testsuite/pulls/1037": + _, _ = w.Write([]byte(`{"number":1037,"head":{"repo":{"full_name":"arteeh/testsuite"}}}`)) + return + case "/repos/o/testsuite/pulls/1089": + _, _ = w.Write([]byte(`{"number":1089,"head":{"repo":{"full_name":"o/testsuite"}}}`)) + return + } + if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/labels") { + var body []string + _ = json.NewDecoder(r.Body).Decode(&body) + mu.Lock() + labelled = append(labelled, r.URL.Path+"="+strings.Join(body, ",")) + mu.Unlock() + _, _ = w.Write([]byte(`[{"name":"x"}]`)) + return + } + w.WriteHeader(http.StatusNotFound) + })) + defer srv.Close() + c := issueTestClient(t, srv.URL) + c.appAuth = &AppAuth{} + c.SetWorkflowsLaneFunc(func(agent, repo string) string { + if repo == "o/testsuite" { + return "scanner" + } + return "" + }) + + released, err := c.SweepWorkflowsBlockers(context.Background()) + if err != nil { + t.Fatalf("sweep: %v", err) + } + got := []int{} + for _, e := range released { + got = append(got, e.Number) + } + if len(got) != 2 || got[0] != 1059 || got[1] != 1089 { + t.Fatalf("released = %v, want o/testsuite#1059 (issue) and #1089 (same-repo PR)", got) + } + if _, blocked := l.Blocking("o/testsuite", 1037); !blocked { + t.Fatal("a fork PR's workflows blocker must stay: the App cannot push workflow files to a fork") + } + if _, blocked := l.Blocking("o/testsuite", 1059); blocked { + t.Fatal("#1059 still blocked") + } + if _, blocked := l.Blocking("o/other", 7); !blocked { + t.Fatal("o/other#7 has no capable lane and must stay blocked") + } + if _, blocked := l.Blocking("o/testsuite", 1073); !blocked { + t.Fatal("a human-only blocker is never re-checked away") + } + if _, blocked := l.Blocking("o/testsuite", 1080); !blocked { + t.Fatal("a workflows grant must not release another capability's blocker") + } + mu.Lock() + defer mu.Unlock() + if len(labelled) != 2 || labelled[0] != "/repos/o/testsuite/issues/1059/labels="+WorkflowsLaneLabel { + t.Fatalf("labels written = %v", labelled) + } + + // Once the hive can push, a fresh report of the same claim is a reason, + // not a blocker. + l.SetCapabilityCheck(c.CapabilityAvailable) + if e, _ := l.Observe("o/testsuite", 1059, "needs-human: the hive app cannot push `.github/workflows/e2e.yml`", time.Now().Add(time.Hour)); e.Blocks() { + t.Fatalf("re-report recorded a blocker: %+v", e) + } +} + +// A merger-tier lane filing a workflow-only issue gets it created unparked, +// actionable, with no routing label (it stays its own) and the note. +func TestCreateWorkflowsParkRefusedForMergerTier(t *testing.T) { + stubInstallation(t, map[string]string{"workflows": "write"}, nil) + var mu sync.Mutex + var got []issueCreatePayload + srv := newIssueCreateLabelServer(t, &mu, &got) + defer srv.Close() + c := issueTestClient(t, srv.URL) + c.SetAgentIssueDecisionPolicy("escalate") + fleet(true, true)(c) + dir := withIssueDir(t) + if _, err := WriteIssueRequest(dir, IssueRequest{ + Repo: "o/r", Title: "[scanner] ponytail daemon dies with the SSH session", + Body: selfPush1059, Labels: []string{"agent/scanner"}, Agent: "scanner", NeedsDecision: true, + }); err != nil { + t.Fatal(err) + } + c.ProcessIssueRequestsOnce(context.Background()) + mu.Lock() + defer mu.Unlock() + if len(got) != 1 { + t.Fatalf("creates = %d, want 1", len(got)) + } + p := got[0] + if hasLabelFold(p.Labels, NeedsDecisionStageLabel) || hasLabelFold(p.Labels, WorkflowsLaneLabel) || !hasLabelFold(p.Labels, "stage/010-actionable") { + t.Fatalf("labels = %v, want actionable, not parked, not handed off", p.Labels) + } + if strings.Contains(p.Body, unparkOwnerStepMarker) || !strings.Contains(p.Body, "Not parked: `scanner`'s token can push") { + t.Fatalf("body must carry the self-push note and no owner-step marker:\n%s", p.Body) + } +} + +func TestWorkflowsRejectionProvesByReporterTier(t *testing.T) { + c := &Client{} + c.SetWorkflowsSelfPushFunc(func(agent string) bool { return agent == "scanner" }) + quote := "refusing to allow a GitHub App to create or update workflow `.github/workflows/e2e.yml` without `workflows` permission" + for text, want := range map[string]bool{ + quote + "\n\n— hive: agent=scanner backend=claude": true, + quote + "\n\n— hive: agent=scanner backend=claude\n": true, + quote + "\n\n— hive: agent=ci-maintainer backend=claude": false, + quote: true, + } { + if got := c.WorkflowsRejectionProves(text); got != want { + t.Errorf("WorkflowsRejectionProves(%q) = %v, want %v", text, got, want) + } + } +} diff --git a/src/pkg/scheduler/policy_overlays.go b/src/pkg/scheduler/policy_overlays.go index e6711c2f23..2786b4ab82 100644 --- a/src/pkg/scheduler/policy_overlays.go +++ b/src/pkg/scheduler/policy_overlays.go @@ -104,6 +104,9 @@ func (s *Scheduler) isHoldGatedPRAgent(agentName string) bool { // the same reason (kubestellar/hive#4744): a customized or remotely sourced // policy must not be able to omit it. func (s *Scheduler) addWorkflowPushCeiling(agentName, message string) string { + if message != "" && s.cfg.AgentPushesWorkflows(agentName) { + return insertAfterKickHeader(message, workflowPushReach) + } if message == "" || s.agentEffectiveMode(agentName) != "ISSUES_AND_PRS" { return message } @@ -159,6 +162,21 @@ serves the repository, the park stands and the owner applies it. return insertAfterKickHeader(message, section) } +// workflowPushReach is the other side of the ceiling: a trusted/merger-tier +// token asks for the Workflows permission, so the lane can push +// .github/workflows/** itself. projectbluefin/testsuite#1059: scanner, at +// ISSUES_PRS_MERGE, parked "the hive app cannot push .github/workflows/" the +// same day the App pushed e2e.yml; nothing in its kick said otherwise. The +// relay refuses such a park when the installation grants the permission. +const workflowPushReach = `## Workflow files are in reach at this mode + +This agent's token is minted at the trusted/merger tier, which asks for the +Workflows permission, so it can push changes under ` + "`.github/workflows/**`" + `. +Push a workflow fix like any other; do not park it as "the App cannot push +workflow files". Only if GitHub rejects the push for the ` + "`workflows`" + ` +permission, park it and quote the rejection. +` + // routeWorkflowsLane sends an issue the relay handed off for a workflow-only // change (github.WorkflowsLaneLabel) to the lane that can push workflow files. // Without it the issue keeps its `[lane]` title / `agent/` routing and diff --git a/src/pkg/scheduler/workflow_push_ceiling_test.go b/src/pkg/scheduler/workflow_push_ceiling_test.go index f31b61bcad..ff7cd6c139 100644 --- a/src/pkg/scheduler/workflow_push_ceiling_test.go +++ b/src/pkg/scheduler/workflow_push_ceiling_test.go @@ -84,3 +84,28 @@ func TestWorkflowPushCeilingSilentForUnknownAgent(t *testing.T) { t.Fatalf("ceiling stated for an agent with no configured mode:\n%s", msg) } } + +// projectbluefin/testsuite#1059: a merger-tier lane is told its token can +// push workflow files; a contributor-tier lane is not. +func TestWorkflowPushReachForMergerTier(t *testing.T) { + for _, tc := range []struct { + mode string + want bool + }{ + {mode: "ISSUES_PRS_MERGE", want: true}, + {mode: "ISSUES_AND_PRS", want: false}, + {mode: "ISSUES_ONLY", want: false}, + } { + t.Run(tc.mode, func(t *testing.T) { + s := heldPRCoordinationScheduler(t, 4, tc.mode) + msg := s.BuildAgentMessage("quality", nil, &github.ActionableResult{}) + got := strings.Contains(msg, "Workflow files are in reach at this mode") && strings.Contains(msg, "do not park it as") + if got != tc.want { + t.Fatalf("mode %s: reach stated = %v, want %v:\n%s", tc.mode, got, tc.want, msg) + } + if tc.want && strings.Index(msg, "Workflow files are in reach") > strings.Index(msg, "CUSTOM POLICY BODY") { + t.Fatalf("the line must precede policy work selection:\n%s", msg) + } + }) + } +}