From 543ed7990f85728c4b9d1a722dd4dbe5023d80fb Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 15:53:02 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Repair=20queues:=20do=20not=20pu?= =?UTF-8?q?sh=20onto=20busy=20non-App=20PR=20branches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The scanner reached owner-session PRs as unattributed FIX-BEFORE-NEW rows (they default to scanner) and pushed its own commits onto branches another worker was still pushing to, turning 'Sign-off survives the squash' red (#18419), racing the worker's next push and duplicating its fix. A red or conflicted PR the hive App did not open is now listed as 'suggest, do not push' in FIX-BEFORE-NEW, the CI_FAILING push queue and PR_LIST while its branch is busy: a live claim by another worker on the PR or a linked issue (worker-claim ledger), or a head pushed within HIVE_SCANNER_BUSY_BRANCH_MINUTES (default 60, from the head's earliest CI activity, no new GitHub calls). App-authored lane branches, in-place adoptions and idle branches are listed exactly as before. Fixes #18488 Signed-off-by: Andy Anderson --- .../fixed-scanner-skip-busy-branches.md | 1 + src/cmd/hive/busy_branch_claims_test.go | 45 +++ src/cmd/hive/claimswire.go | 31 ++ src/cmd/hive/main.go | 3 + src/cmd/hive/merge_eligibility.go | 11 + src/docs/env-vars.md | 1 + src/pkg/scheduler/busy_branch.go | 288 ++++++++++++++++ src/pkg/scheduler/busy_branch_test.go | 311 ++++++++++++++++++ src/pkg/scheduler/kickmessage.go | 3 +- src/pkg/scheduler/lane_messages.go | 68 +++- src/pkg/scheduler/policy_overlays.go | 86 +++-- src/pkg/scheduler/scheduler.go | 1 + src/pkg/scheduler/templates.go | 2 +- 13 files changed, 807 insertions(+), 44 deletions(-) create mode 100644 changelog.d/fixed-scanner-skip-busy-branches.md create mode 100644 src/cmd/hive/busy_branch_claims_test.go create mode 100644 src/pkg/scheduler/busy_branch.go create mode 100644 src/pkg/scheduler/busy_branch_test.go diff --git a/changelog.d/fixed-scanner-skip-busy-branches.md b/changelog.d/fixed-scanner-skip-busy-branches.md new file mode 100644 index 0000000000..d297d07af2 --- /dev/null +++ b/changelog.d/fixed-scanner-skip-busy-branches.md @@ -0,0 +1 @@ +- The scanner (and every repair queue) no longer pushes onto a PR branch the hive App did not open while someone else is working on it ([#18488](https://github.com/hivecommons/hive/issues/18488)). A red or conflicted non-App PR whose head was pushed within `HIVE_SCANNER_BUSY_BRANCH_MINUTES` (default 60), or whose PR or linked issue another worker holds a live claim on, moves from FIX-BEFORE-NEW and the CI_FAILING push queue to a "BUSY BRANCHES — suggest, do not push" list, and the agent posts the fix as one suggestion comment for the worker to apply as its own signed commit. Idle branches, App-authored lane branches and in-place adoptions are listed exactly as before. diff --git a/src/cmd/hive/busy_branch_claims_test.go b/src/cmd/hive/busy_branch_claims_test.go new file mode 100644 index 0000000000..1b6d0f4f16 --- /dev/null +++ b/src/cmd/hive/busy_branch_claims_test.go @@ -0,0 +1,45 @@ +package main + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/claims" +) + +// hivecommons/hive#18488: the busy-branch guard reads real claims from the +// worker-claim ledger — never kick listings — and reports which hive agent +// holds one so the kicked agent's own claim is not mistaken for another +// worker's. + +func TestClaimsPRLeaseLookup(t *testing.T) { + var none *claims.Ledger + if claimsPRLeaseLookup(none, "hivecommons") != nil { + t.Fatal("no ledger must install no lookup") + } + l, err := claims.New(filepath.Join(t.TempDir(), "claims.json"), claims.DefaultPolicy(), claims.Hooks{}) + if err != nil { + t.Fatal(err) + } + for _, req := range []claims.Request{ + {Repo: "hivecommons/hive", Issue: 18488, Holder: "quality", HolderID: "quality", Kind: claims.KindAgent}, + {Repo: "hivecommons/hive", Issue: 18441, Holder: "clubanderson", Kind: claims.KindHuman}, + } { + if res, err := l.Claim(req); err != nil || res.Outcome != claims.OutcomeClaimed { + t.Fatalf("claim %+v: outcome=%s err=%v", req, res.Outcome, err) + } + } + lookup := claimsPRLeaseLookup(l, "hivecommons") + // A bare governor repo name is qualified with the org, as the in-flight + // lookup does. + if holder, agent, ok := lookup("hive", 18488); !ok || agent != "quality" || !strings.Contains(holder, "quality") { + t.Fatalf("agent claim: holder=%q agent=%q ok=%v", holder, agent, ok) + } + if holder, agent, ok := lookup("hivecommons/hive", 18441); !ok || agent != "" || !strings.Contains(holder, "clubanderson") { + t.Fatalf("human claim: holder=%q agent=%q ok=%v (a person is never reported as a hive agent)", holder, agent, ok) + } + if _, _, ok := lookup("hivecommons/hive", 1); ok { + t.Fatal("an unclaimed number must not read as busy") + } +} diff --git a/src/cmd/hive/claimswire.go b/src/cmd/hive/claimswire.go index ddb4cdd1f2..133cb34467 100644 --- a/src/cmd/hive/claimswire.go +++ b/src/cmd/hive/claimswire.go @@ -459,6 +459,37 @@ func claimsInflightLookup(ledger *claims.Ledger, org string) scheduler.InflightL } } +// claimsPRLeaseLookup adapts the worker-claim ledger to the scheduler's +// busy-branch guard (hivecommons/hive#18488): a live claim on a PR or its +// linked issue means a worker is on that branch. Only real claims count — +// a kick listing is not a worker — and the holding agent is reported so the +// guard can ignore the kicked agent's own claim. +func claimsPRLeaseLookup(ledger *claims.Ledger, org string) scheduler.PRLeaseLookup { + if ledger == nil { + return nil + } + return func(repo string, number int) (string, string, bool) { + if number <= 0 { + return "", "", false + } + c, ok := ledger.Lookup(repo, number) + if !ok && org != "" && !strings.Contains(repo, "/") { + c, ok = ledger.Lookup(org+"/"+repo, number) + } + if !ok { + return "", "", false + } + holderAgent := "" + if c.Kind == claims.KindAgent { + holderAgent = c.HolderID + if holderAgent == "" { + holderAgent = c.Holder + } + } + return fmt.Sprintf("%s (%s) until %s", c.Holder, c.Kind, c.ExpiresAt.UTC().Format(time.RFC3339)), holderAgent, true + } +} + // composeInflight ORs several in-flight lookups; nil entries are skipped. func composeInflight(lookups ...scheduler.InflightLookup) scheduler.InflightLookup { var live []scheduler.InflightLookup diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 71ab7af0e0..4c41ecfac3 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -3512,6 +3512,9 @@ func (b *boot) bootSupervision() { contributorClaimOverrideLookup(contributorClaimGuard.Load, claimsInflightLookup(b.issueClaims, b.cfg.Project.Org)), verifyOnceLookup(b.ctx, b.cfg.Project.Org, b.issueClaims, func() *github.Client { return b.ghClient }, b.logger), claimEscalationLookup(b.ctx, b.cfg, b.issueClaims, func() *github.Client { return b.ghClient }, b.logger))) + // #18488: a live claim on a non-App PR (or its linked issue) keeps the + // repair queues from sending an agent to push onto that branch. + b.sched.SetPRLeaseLookup(claimsPRLeaseLookup(b.issueClaims, b.cfg.Project.Org)) // Dashboard maintainer notes on a listed item ride its kick as private // context (pkg/scheduler/kick_maintainer_notes.go). b.sched.SetMaintainerNotesLookup(maintainernotes.Default().List) diff --git a/src/cmd/hive/merge_eligibility.go b/src/cmd/hive/merge_eligibility.go index abe876ee34..0bb984043a 100644 --- a/src/cmd/hive/merge_eligibility.go +++ b/src/cmd/hive/merge_eligibility.go @@ -19,6 +19,7 @@ import ( "github.com/hivecommons/hive/pkg/intent" "github.com/hivecommons/hive/pkg/rebasemerge" "github.com/hivecommons/hive/pkg/review" + "github.com/hivecommons/hive/pkg/scheduler" "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -1121,6 +1122,13 @@ func writeMergeEligible(actionable *github.ActionableResult, hold github.HoldRes // needs GitHub's own verdict to pick REPAIR vs DE-ESCALATE. Mergeable string `json:"mergeable,omitempty"` CIStatus string `json:"ci_status,omitempty"` + // AppAuthored, HeadPushedAt and Issues are the busy-branch evidence + // (hivecommons/hive#18488): a repair queue must not send an agent to + // push onto a branch that is not the hive App's own while another + // worker is on it. See pkg/scheduler/busy_branch.go. + AppAuthored bool `json:"app_authored,omitempty"` + HeadPushedAt *time.Time `json:"head_pushed_at,omitempty"` + Issues []int `json:"issues,omitempty"` } prAgents := auditPRAgents(org, time.Now().Add(-auditPRAttributionWindow), "") @@ -1258,6 +1266,9 @@ func writeMergeEligible(actionable *github.ActionableResult, hold github.HoldRes Held: held, BaseRedChecks: pr.BaseRedChecks, BaseRed: pr.AllFailuresBaseRed(), + AppAuthored: scheduler.PRAppAuthored(pr), + HeadPushedAt: scheduler.PRHeadPushedAt(pr), + Issues: scheduler.PRIssueNumbers(pr), } if adopted { row.ReachableAction = github.ReachableActionPush diff --git a/src/docs/env-vars.md b/src/docs/env-vars.md index d2aa2ffe0d..01ad1696c6 100644 --- a/src/docs/env-vars.md +++ b/src/docs/env-vars.md @@ -65,6 +65,7 @@ behavior. [Maintenance limitations](#keeping-this-reference-current) still apply | `HIVE_REPO` | No | none | Bootstrap shortcut in `owner/repo` form; fills `project.org`, `project.repos`, and `project.primary_repo` if missing. | | `HIVE_RELEASE_SENTINEL_ENABLED` | No | `release_sentinel.enabled` (default `false`) | Process-level override for the opt-in release sentinel ([#9585](https://github.com/hivecommons/hive/issues/9585)). `true`/`1`/`on`/`yes` turns it on and `false`/`0`/`off`/`no` turns it off regardless of config; any other value leaves the config in charge. See [release-sentinel.md](release-sentinel.md). | | `HIVE_RELEASE_SENTINEL_RETAG_ENABLED` | No | `release_sentinel.retag_enabled` (default `false`) | Process-level override for the release sentinel's separate retag opt-in ([#9585](https://github.com/hivecommons/hive/issues/9585)): when on, the hive moves the `v` tag to a merged, marked fix PR's merge commit with one leased, atomic tag push. Same values as `HIVE_RELEASE_SENTINEL_ENABLED`. It never turns retagging on while the sentinel itself is off. See [release-sentinel.md](release-sentinel.md#retag-after-merge). | +| `HIVE_SCANNER_BUSY_BRANCH_MINUTES` | No | `60` | Busy-branch window for the repair queues ([#18488](https://github.com/hivecommons/hive/issues/18488)). A red or conflicted PR the hive App did not open is listed as "suggest, do not push" instead of as a push target while its head is younger than this many minutes or a claim by another worker covers the PR or a linked issue; the agent posts the fix as one suggestion comment for the worker to apply as its own signed commit. Whole minutes; unset, unparsable or non-positive values keep `60`. | | `HIVE_ATTRIBUTED_CLOSED_PR_LOOKBACK` | No | `14d` | Bounds the per-repo closed-PR attribution scan that feeds dashboard outcome/rework metrics during PR enumeration. The scan lists closed PRs sorted by `updated_at` descending, stops when results are older than this window, and also stops after five pages so large repositories are never walked from history on every governor tick. Values accept Go durations such as `336h`, or day counts such as `14`/`14d`; invalid or non-positive values fall back to `14d`. | | `HIVE_LEVEL` | No | config/pack value | ACMM level bootstrap/override used by hosted flows and the entrypoint pack selection. | | `HIVE_ID` | No | config or generated id | Stable hive/spoke identifier override; passed through to launched agents. | diff --git a/src/pkg/scheduler/busy_branch.go b/src/pkg/scheduler/busy_branch.go new file mode 100644 index 0000000000..67a07d04a5 --- /dev/null +++ b/src/pkg/scheduler/busy_branch.go @@ -0,0 +1,288 @@ +package scheduler + +import ( + "fmt" + "os" + "sort" + "strconv" + "strings" + "time" + + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Busy-branch guard for the repair queues (hivecommons/hive#18488). +// +// The FIX-BEFORE-NEW block and the shared CI_FAILING queue list red or +// conflicted PRs as push targets. A PR the hive App did not open — the +// owner's own session PRs, a same-repo contributor branch — reached the +// scanner as "yours" (unattributed rows default to scanner) and the scanner +// pushed its own commits onto a branch another worker was still pushing to: +// the "Sign-off survives the squash" check went red (#18419), the worker's +// next push raced, and it re-made the same fix. The decision point is the +// same one that already keeps fork PRs comment-only: such a row is listed +// as "suggest, do not push" when the branch is busy, i.e. when +// +// - a live claim held by someone other than the kicked agent covers the +// PR or one of its linked issues (the claims ledger), or +// - the head was pushed less than busyBranchWindow() ago. +// +// Rows of App-authored PRs and in-place adoptions are never checked: the +// first are the hive's own lane branches, the second are owned by the hive +// for as long as the adoption holds. A row with no claim and no recent push +// is idle and keeps its push instruction exactly as before. + +// DefaultBusyBranchMinutes is how recent a head push makes a non-App branch +// busy. BusyBranchMinutesEnv overrides it; a missing, unparsable or +// non-positive value keeps the default. +const ( + DefaultBusyBranchMinutes = 60 + BusyBranchMinutesEnv = "HIVE_SCANNER_BUSY_BRANCH_MINUTES" +) + +// busyBranchNow is the busy guard's clock; tests swap it. +var busyBranchNow = time.Now + +// busyBranchWindow is the recent-push window in effect. +func busyBranchWindow() time.Duration { + minutes := DefaultBusyBranchMinutes + if raw := strings.TrimSpace(os.Getenv(BusyBranchMinutesEnv)); raw != "" { + if n, err := strconv.Atoi(raw); err == nil && n > 0 { + minutes = n + } + } + return time.Duration(minutes) * time.Minute +} + +// PRLeaseLookup reports a live claim on repo#number. holder is a short +// display string ("clubanderson (human) until …"); holderAgent is the hive +// agent name when a hive agent holds it, else "". +type PRLeaseLookup func(repo string, number int) (holder, holderAgent string, held bool) + +// SetPRLeaseLookup installs (or with nil, removes) the claim lookup the busy +// guard consults. +func (s *Scheduler) SetPRLeaseLookup(fn PRLeaseLookup) { + s.mu.Lock() + defer s.mu.Unlock() + s.prLease = fn +} + +// busyBranchRow is the part of a ci-failing.json row the guard reads. +type busyBranchRow struct { + Repo string + Number int + Issues []int + AppAuthored bool + Adopted bool + HeadPushedAt *time.Time + // Own marks a row the audit trail attributes to the kicked agent itself + // (a lane PR opened on a person's credentials): its recent pushes are + // that agent's own, so only another worker's claim makes it busy. + Own bool +} + +// ownRepairRow reports a ci-failing.json row attributed to agent by the +// audit trail itself. A trusted-author rebase or a reroute from a paused lane +// names its fixer explicitly but is still someone else's branch. +func ownRepairRow(rowAgent, agent string, trustedAuthor bool, reroutedFrom string) bool { + rowAgent = strings.TrimSpace(rowAgent) + return rowAgent != "" && agent != "" && strings.EqualFold(rowAgent, agent) && !trustedAuthor && strings.TrimSpace(reroutedFrom) == "" +} + +// busyBranchCheck returns why a row's branch is busy for agent, or "" when it +// is free to push to. A nil check treats every branch as free. +type busyBranchCheck func(row busyBranchRow) string + +// busyBranchChecker builds the guard for agent (a base agent name; "" means +// any hive agent's claim counts as another worker's). +func (s *Scheduler) busyBranchChecker(agent string) busyBranchCheck { + var lease PRLeaseLookup + if s != nil { + s.mu.RLock() + lease = s.prLease + s.mu.RUnlock() + } + baseName := func(name string) string { + if s == nil || s.cfg == nil { + return name + } + return s.cfg.BaseAgentName(name) + } + window := busyBranchWindow() + now := busyBranchNow() + return func(row busyBranchRow) string { + return busyBranchReason(row, agent, lease, baseName, window, now) + } +} + +// busyBranchReason is the guard's decision, separated from the scheduler so +// tests drive it with fixed inputs. +func busyBranchReason(row busyBranchRow, agent string, lease PRLeaseLookup, baseName func(string) string, window time.Duration, now time.Time) string { + if row.AppAuthored || row.Adopted { + return "" + } + if lease != nil { + for _, n := range append([]int{row.Number}, row.Issues...) { + if n <= 0 { + continue + } + holder, holderAgent, held := lease(row.Repo, n) + if !held { + continue + } + if holderAgent != "" && agent != "" && strings.EqualFold(baseName(holderAgent), agent) { + continue // the kicked agent's own claim is not another worker + } + what := "this PR" + if n != row.Number { + what = fmt.Sprintf("linked issue #%d", n) + } + return fmt.Sprintf("%s is claimed by %s", what, holder) + } + } + if !row.Own && row.HeadPushedAt != nil && !row.HeadPushedAt.IsZero() { + if age := now.Sub(*row.HeadPushedAt); age < window { + if age < 0 { + age = 0 + } + return fmt.Sprintf("head pushed %s ago (busy window %s)", age.Round(time.Minute), window) + } + } + return "" +} + +// busyBranchInstruction is what an agent does with a busy branch instead of +// pushing: hand the fix to the worker so it lands as that worker's own +// signed commit. +const busyBranchInstruction = "Do NOT push, rebase or force-push these branches: someone else is working on them, and a commit from you would break their sign-off check and race their next push. " + + "Post the fix as a suggestion instead — a GitHub suggestion block, a diff, or exact steps — in ONE comment per PR: if you already left a note on that PR, edit it rather than adding another. " + + "The worker applies it as its own signed commit. A branch that stays idle returns to the push list once its head is older than the busy window and no claim covers it." + +// busyBranchAnnotation is the ${PR_LIST} marker for a busy branch, the +// counterpart of forkAnnotation's "comment only, cannot push": the agent +// learns from its work list that it must not push there. Empty for a fork PR +// (already comment-only) and for a free branch. +func busyBranchAnnotation(pr github.PullRequest, agent string, busy busyBranchCheck) string { + if busy == nil || pr.FromFork { + return "" + } + why := busy(busyBranchRow{ + Repo: pr.Repo, + Number: pr.Number, + Issues: PRIssueNumbers(pr), + AppAuthored: PRAppAuthored(pr), + Adopted: prHasLabel(pr.Labels, stagelabels.AdoptedByHive.Label()), + HeadPushedAt: PRHeadPushedAt(pr), + Own: ownRepairRow(pr.HiveAgent, agent, false, ""), + }) + if why == "" { + return "" + } + return " [busy: " + why + " — do not push; post the fix as one suggestion comment]" +} + +// busyRedPR is one busy row as the kick renders it. +type busyRedPR struct { + Number int + Repo string + Title string + FailingChecks []string + Excerpt string + Why string +} + +// writeBusyRedPRs renders the FIX-BEFORE-NEW companion block for busy rows: +// the evidence an agent needs to write the suggestion, under a heading that +// says the branch is not theirs to push to. n rows are read through row. +func writeBusyRedPRs(b *strings.Builder, n int, row func(i int) busyRedPR) { + if n == 0 { + return + } + fmt.Fprintf(b, "\n## ⏸ BUSY BRANCHES — red PRs you must NOT push to (%d)\n\n", n) + b.WriteString(busyBranchInstruction + "\n\n") + for i := 0; i < n; i++ { + if i >= redPRFixMaxDetailed { + fmt.Fprintf(b, " … and %d more (full list: %s)\n", n-i, ciFailingPath) + break + } + pr := row(i) + fmt.Fprintf(b, " #%d %s — %s\n", pr.Number, pr.Repo, pr.Title) + b.WriteString(" busy: " + pr.Why + " — suggest, do not push\n") + if len(pr.FailingChecks) > 0 { + fmt.Fprintf(b, " failing: %s\n", strings.Join(pr.FailingChecks, ", ")) + } + if excerpt := strings.TrimSpace(pr.Excerpt); excerpt != "" { + if runes := []rune(excerpt); len(runes) > redPRFixExcerptRunes { + excerpt = string(runes[:redPRFixExcerptRunes]) + "…" + } + b.WriteString(" evidence: " + strings.ReplaceAll(excerpt, "\n", "\n ") + "\n") + } + } + b.WriteString("\n") +} + +// Busy-branch evidence from a PR record. cmd/hive stamps these on +// ci-failing.json rows and the PR list reads them directly; all come from data +// the eval tick already holds, so no GitHub call is added. + +// PRAppAuthored reports a PR the hive App itself opened. Only such a branch +// is the hive's own to push to without asking whether someone else is working +// on it; a PR opened on a person's credentials (HiveAttributed) is that +// person's branch. +func PRAppAuthored(pr github.PullRequest) bool { + return pr.AppAuthored || strings.HasPrefix(strings.ToLower(strings.TrimSpace(pr.Author)), "app/") +} + +// PRHeadPushedAt approximates when the current head was pushed: the +// earliest CI activity GitHub reported on that head (a check run starting or +// finishing, a commit status being posted). CI reacts to a push within +// moments, so this is a close upper bound on the push time; nil when no CI +// activity was observed, which the consumer reads as "no recent push known". +func PRHeadPushedAt(pr github.PullRequest) *time.Time { + if pr.CIActivity == nil { + return nil + } + head := strings.TrimSpace(pr.HeadSHA) + var earliest *time.Time + for _, check := range pr.CIActivity.Checks { + if check.Since == nil || check.Since.IsZero() { + continue + } + if check.Head != "" && head != "" && !strings.EqualFold(check.Head, head) { + continue + } + if earliest == nil || check.Since.Before(*earliest) { + at := check.Since.UTC() + earliest = &at + } + } + return earliest +} + +// PRIssueNumbers lists the same-repo issues the PR is linked to (GitHub's +// closing references and the PR's own references), sorted and de-duplicated, +// so a consumer can ask the claims ledger whether a worker holds one. +func PRIssueNumbers(pr github.PullRequest) []int { + bare := strings.ToLower(strings.TrimSpace(pr.Repo)) + if i := strings.LastIndex(bare, "/"); i >= 0 { + bare = bare[i+1:] + } + sameRepo := func(repo string) bool { + repo = strings.ToLower(strings.TrimSpace(repo)) + return repo == "" || repo == bare || strings.HasSuffix(repo, "/"+bare) + } + seen := map[int]bool{} + var out []int + for _, list := range [][]github.PRLinkedIssue{pr.LinkedIssues, pr.ReferencedIssues} { + for _, issue := range list { + if issue.Number <= 0 || issue.Number == pr.Number || seen[issue.Number] || !sameRepo(issue.Repo) { + continue + } + seen[issue.Number] = true + out = append(out, issue.Number) + } + } + sort.Ints(out) + return out +} diff --git a/src/pkg/scheduler/busy_branch_test.go b/src/pkg/scheduler/busy_branch_test.go new file mode 100644 index 0000000000..fcc1772fb1 --- /dev/null +++ b/src/pkg/scheduler/busy_branch_test.go @@ -0,0 +1,311 @@ +package scheduler + +// hivecommons/hive#18488: the scanner pushed its own commits onto PR +// branches another worker was still pushing to — an owner-session PR it +// reached as an unattributed "yours" row in FIX-BEFORE-NEW — and the +// "Sign-off survives the squash" check went red (#18419). These tests pin the +// invariant: a non-App branch that is busy (a live claim by someone else, or +// a head pushed inside the busy window) is never listed as a push target in +// either repair queue, and the same branch once idle is listed exactly as +// before. + +import ( + "fmt" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" +) + +var busyTestNow = time.Date(2026, 11, 16, 16, 0, 0, 0, time.UTC) + +func busyPushedAt(ago time.Duration) *time.Time { + at := busyTestNow.Add(-ago) + return &at +} + +func identityBase(name string) string { return name } + +func TestBusyBranchReason(t *testing.T) { + window := time.Duration(DefaultBusyBranchMinutes) * time.Minute + leaseOn := func(repo string, number int, holder, holderAgent string) PRLeaseLookup { + return func(r string, n int) (string, string, bool) { + if r == repo && n == number { + return holder, holderAgent, true + } + return "", "", false + } + } + const repo = "test-org/hive" + cases := []struct { + name string + row busyBranchRow + lease PRLeaseLookup + busy bool + }{ + {"idle: no claim, old push", busyBranchRow{Repo: repo, Number: 8, HeadPushedAt: busyPushedAt(2 * window)}, nil, false}, + {"idle: no claim, no push evidence", busyBranchRow{Repo: repo, Number: 8}, leaseOn(repo, 99, "x", ""), false}, + {"busy: head pushed inside the window", busyBranchRow{Repo: repo, Number: 8, HeadPushedAt: busyPushedAt(5 * time.Minute)}, nil, true}, + {"busy: PR claimed by a person", busyBranchRow{Repo: repo, Number: 8}, leaseOn(repo, 8, "clubanderson (human)", ""), true}, + {"busy: linked issue claimed by another agent", busyBranchRow{Repo: repo, Number: 8, Issues: []int{18488}}, leaseOn(repo, 18488, "quality (agent)", "quality"), true}, + {"idle: the kicked agent's own claim", busyBranchRow{Repo: repo, Number: 8, Issues: []int{18488}}, leaseOn(repo, 18488, "scanner (agent)", "scanner"), false}, + {"own row: its own recent push is not another worker", busyBranchRow{Repo: repo, Number: 8, Own: true, HeadPushedAt: busyPushedAt(time.Minute)}, nil, false}, + {"own row: another worker's claim still counts", busyBranchRow{Repo: repo, Number: 8, Own: true}, leaseOn(repo, 8, "clubanderson (human)", ""), true}, + {"App-authored branch is never checked", busyBranchRow{Repo: repo, Number: 8, AppAuthored: true, HeadPushedAt: busyPushedAt(time.Minute)}, leaseOn(repo, 8, "clubanderson (human)", ""), false}, + {"adopted branch is never checked", busyBranchRow{Repo: repo, Number: 8, Adopted: true, HeadPushedAt: busyPushedAt(time.Minute)}, leaseOn(repo, 8, "clubanderson (human)", ""), false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + why := busyBranchReason(tc.row, "scanner", tc.lease, identityBase, window, busyTestNow) + if got := why != ""; got != tc.busy { + t.Fatalf("busy = %v (reason %q), want %v", got, why, tc.busy) + } + }) + } +} + +func TestBusyBranchWindow_EnvOverride(t *testing.T) { + t.Setenv(BusyBranchMinutesEnv, "") + if got := busyBranchWindow(); got != time.Duration(DefaultBusyBranchMinutes)*time.Minute { + t.Fatalf("default window = %s", got) + } + t.Setenv(BusyBranchMinutesEnv, "15") + if got := busyBranchWindow(); got != 15*time.Minute { + t.Fatalf("override window = %s, want 15m", got) + } + for _, bad := range []string{"0", "-5", "soon"} { + t.Setenv(BusyBranchMinutesEnv, bad) + if got := busyBranchWindow(); got != time.Duration(DefaultBusyBranchMinutes)*time.Minute { + t.Fatalf("%q must keep the default, got %s", bad, got) + } + } +} + +// busyQueueFixture: three unattributed same-repo PRs that are not App-authored +// (each defaults to scanner) — #18419 pushed 5 minutes ago, #18420 whose +// linked issue a person holds, #18421 idle — plus an App-authored #18422 +// pushed a minute ago, which stays the hive's own push target. +func busyQueueFixture() string { + ts := func(ago time.Duration) string { return busyTestNow.Add(-ago).Format(time.RFC3339) } + return fmt.Sprintf(`{"ci_failing":[ + {"number":18419,"repo":"test-org/hive","title":"owner session PR","author":"clubanderson","reachable_action":"push", + "head_pushed_at":%q,"failing_checks":["Sign-off survives the squash"],"excerpt":"undefined: newMinimalServer"}, + {"number":18420,"repo":"test-org/hive","title":"claimed PR","author":"clubanderson","reachable_action":"push", + "head_pushed_at":%q,"issues":[18488],"failing_checks":["build"]}, + {"number":18421,"repo":"test-org/hive","title":"idle PR","author":"clubanderson","reachable_action":"push", + "head_pushed_at":%q,"failing_checks":["lint"]}, + {"number":18422,"repo":"test-org/hive","title":"hive lane PR","author":"hivecommons-hive[bot]","app_authored":true, + "reachable_action":"push","head_pushed_at":%q,"failing_checks":["test"]} +]}`, ts(5*time.Minute), ts(3*time.Hour), ts(3*time.Hour), ts(time.Minute)) +} + +func withBusyQueue(t *testing.T) *Scheduler { + t.Helper() + dir := t.TempDir() + orig, origNow := ciFailingPath, busyBranchNow + ciFailingPath = filepath.Join(dir, "ci-failing.json") + busyBranchNow = func() time.Time { return busyTestNow } + t.Cleanup(func() { ciFailingPath, busyBranchNow = orig, origNow }) + t.Setenv(BusyBranchMinutesEnv, "") + if err := os.WriteFile(ciFailingPath, []byte(busyQueueFixture()), 0o644); err != nil { + t.Fatal(err) + } + s := newScheduler() + s.SetPRLeaseLookup(func(repo string, number int) (string, string, bool) { + if repo == "test-org/hive" && number == 18488 { + return "clubanderson (human) until 2026-11-16T18:00:00Z", "", true + } + return "", "", false + }) + return s +} + +func TestFormatRedPRFixData_BusyBranchesAreNotPushTargets(t *testing.T) { + s := withBusyQueue(t) + out := s.redPRFixSection("scanner") + + busyIdx := strings.Index(out, "BUSY BRANCHES") + if busyIdx < 0 { + t.Fatalf("busy rows must be named in their own block:\n%s", out) + } + gate, busy := out[:busyIdx], out[busyIdx:] + if !strings.Contains(gate, "FIX-BEFORE-NEW") || !strings.Contains(gate, "(2)") { + t.Fatalf("the push gate must hold exactly the idle and App-authored PRs:\n%s", out) + } + for _, want := range []string{"#18421 ", "#18422 "} { + if !strings.Contains(gate, want) { + t.Errorf("%s must stay a push target:\n%s", want, out) + } + } + for _, reject := range []string{"#18419 ", "#18420 "} { + if strings.Contains(gate, reject) { + t.Errorf("busy %s must never be listed under the push gate:\n%s", reject, out) + } + if !strings.Contains(busy, reject) { + t.Errorf("busy %s must be listed as suggest-only:\n%s", reject, out) + } + } + for _, want := range []string{"Do NOT push", "suggestion", "edit it rather than adding another", "head pushed 5m0s ago", "linked issue #18488 is claimed by clubanderson", "undefined: newMinimalServer"} { + if !strings.Contains(busy, want) { + t.Errorf("busy block missing %q:\n%s", want, busy) + } + } + + // The same branches once idle (no claim, head older than the window) + // are push targets again, exactly as before the guard. + s.SetPRLeaseLookup(nil) + busyBranchNow = func() time.Time { return busyTestNow.Add(2 * time.Hour) } + idle := s.redPRFixSection("scanner") + if strings.Contains(idle, "BUSY BRANCHES") { + t.Fatalf("idle branches must not be reported busy:\n%s", idle) + } + for _, want := range []string{"(4)", "#18419 ", "#18420 ", "#18421 ", "#18422 ", "gh pr checkout"} { + if !strings.Contains(idle, want) { + t.Errorf("idle gate missing %q:\n%s", want, idle) + } + } +} + +// Only busy rows: they are listed, but no FIX-BEFORE-NEW gate is raised — +// nothing the agent may push to must not block its new work. +func TestFormatRedPRFixData_OnlyBusyRowsRaiseNoGate(t *testing.T) { + ts := busyTestNow.Add(-time.Minute).Format(time.RFC3339) + data := fmt.Sprintf(`{"ci_failing":[{"number":5,"repo":"o/r","title":"t","author":"alice","reachable_action":"push","head_pushed_at":%q}]}`, ts) + check := func(row busyBranchRow) string { + return busyBranchReason(row, "scanner", nil, identityBase, time.Hour, busyTestNow) + } + out := formatRedPRFixDataGuarded([]byte(data), "scanner", nil, check) + if strings.Contains(out, "FIX-BEFORE-NEW") { + t.Fatalf("a queue of only busy rows must not raise the gate:\n%s", out) + } + if !strings.Contains(out, "BUSY BRANCHES") || !strings.Contains(out, "#5 ") { + t.Fatalf("the busy row must still be named:\n%s", out) + } +} + +func TestBuildCIFailingList_BusyBranchesAreCommentOnly(t *testing.T) { + s := withBusyQueue(t) + out := s.buildCIFailingListForAgent("scanner", nil) + + busyIdx := strings.Index(out, "BUSY BRANCHES (2 — another worker is on these branches; suggest the fix, do not push)") + if busyIdx < 0 { + t.Fatalf("busy section missing:\n%s", out) + } + push, busy := out[:busyIdx], out[busyIdx:] + for _, want := range []string{"#18421 ", "#18422 "} { + if !strings.Contains(push, want) { + t.Errorf("%s must stay in the push queue:\n%s", want, out) + } + } + for _, reject := range []string{"#18419 ", "#18420 "} { + if strings.Contains(push, reject) { + t.Errorf("busy %s must never be in the push queue:\n%s", reject, out) + } + if !strings.Contains(busy, reject) { + t.Errorf("busy %s must be listed as suggest-only:\n%s", reject, out) + } + } + + s.SetPRLeaseLookup(nil) + busyBranchNow = func() time.Time { return busyTestNow.Add(2 * time.Hour) } + idle := s.buildCIFailingListForAgent("scanner", nil) + if strings.Contains(idle, "BUSY BRANCHES") { + t.Fatalf("idle branches must not be reported busy:\n%s", idle) + } + for _, want := range []string{"#18419 ", "#18420 ", "#18421 ", "#18422 "} { + if !strings.Contains(idle, want) { + t.Errorf("idle queue missing %s:\n%s", want, idle) + } + } +} + +// The busy evidence cmd/hive stamps on ci-failing.json rows. + +func TestPRAppAuthored(t *testing.T) { + cases := []struct { + pr github.PullRequest + want bool + }{ + {github.PullRequest{AppAuthored: true}, true}, + {github.PullRequest{Author: "app/hivecommons-hive"}, true}, + // A PR a hive agent opened on a person's credentials is that + // person's branch, not the App's. + {github.PullRequest{Author: "clubanderson", HiveAttributed: true, HiveAgent: "scanner"}, false}, + {github.PullRequest{Author: "alice"}, false}, + } + for _, tc := range cases { + if got := PRAppAuthored(tc.pr); got != tc.want { + t.Errorf("PRAppAuthored(%+v) = %v, want %v", tc.pr, got, tc.want) + } + } +} + +func TestPRHeadPushedAt_EarliestActivityOnCurrentHead(t *testing.T) { + at := func(m int) *time.Time { + v := time.Date(2026, 11, 16, 15, m, 0, 0, time.UTC) + return &v + } + pr := github.PullRequest{HeadSHA: "abc", CIActivity: &github.PRCIActivity{Head: "abc", Checks: []github.PRActivityCheck{ + {Name: "build", Head: "abc", Since: at(30)}, + {Name: "dco", Head: "abc", Since: at(12)}, + {Name: "old head", Head: "zzz", Since: at(1)}, // a previous head's run is not this push + {Name: "queued", Head: "abc"}, + }}} + got := PRHeadPushedAt(pr) + if got == nil || !got.Equal(*at(12)) { + t.Fatalf("head pushed at = %v, want %v", got, at(12)) + } + if PRHeadPushedAt(github.PullRequest{HeadSHA: "abc"}) != nil { + t.Fatal("no CI activity must report no push time, never a guess") + } +} + +func TestPRIssues_SameRepoDeduplicated(t *testing.T) { + pr := github.PullRequest{Repo: "hive", Number: 18419, + LinkedIssues: []github.PRLinkedIssue{{Number: 18488, Repo: "hivecommons/hive"}, {Number: 7, Repo: "other/repo"}}, + ReferencedIssues: []github.PRLinkedIssue{{Number: 18441}, {Number: 18488}, {Number: 18419}}, + } + if got, want := PRIssueNumbers(pr), []int{18441, 18488}; !reflect.DeepEqual(got, want) { + t.Fatalf("issues = %v, want %v", got, want) + } +} + +// ${PR_LIST} carries the same verdict inline, next to the fork marker, so an +// agent working the PR list (CI repair step 4, conflict rebases) learns from +// the list itself that a busy branch is not its to push to. +func TestFormatPRList_AnnotatesBusyBranches(t *testing.T) { + orig := busyBranchNow + busyBranchNow = func() time.Time { return busyTestNow } + t.Cleanup(func() { busyBranchNow = orig }) + t.Setenv(BusyBranchMinutesEnv, "") + activity := func(ago time.Duration) *github.PRCIActivity { + return &github.PRCIActivity{Head: "h", Checks: []github.PRActivityCheck{{Name: "dco", Head: "h", Since: busyPushedAt(ago)}}} + } + actionable := &github.ActionableResult{PRs: github.PRResult{Items: []github.PullRequest{ + {Repo: "hive", Number: 18419, Title: "owner session PR", Author: "clubanderson", HeadSHA: "h", CIActivity: activity(5 * time.Minute)}, + {Repo: "hive", Number: 18421, Title: "idle owner PR", Author: "clubanderson", HeadSHA: "h", CIActivity: activity(3 * time.Hour)}, + {Repo: "hive", Number: 18422, Title: "hive lane PR", Author: "hivecommons-hive[bot]", AppAuthored: true, HeadSHA: "h", CIActivity: activity(time.Minute)}, + }}} + s := newScheduler() + out, _ := s.formatPRListWithPolicyForAgent(actionable, "scanner") + lines := map[int]string{} + for _, line := range strings.Split(out, "\n") { + for _, n := range []int{18419, 18421, 18422} { + if strings.Contains(line, fmt.Sprintf("#%d ", n)) { + lines[n] = line + } + } + } + if !strings.Contains(lines[18419], "[busy: head pushed 5m0s ago") || !strings.Contains(lines[18419], "do not push") { + t.Errorf("busy PR must be annotated do-not-push: %q", lines[18419]) + } + for _, n := range []int{18421, 18422} { + if lines[n] == "" || strings.Contains(lines[n], "[busy:") { + t.Errorf("#%d must be listed without a busy marker: %q\n%s", n, lines[n], out) + } + } +} diff --git a/src/pkg/scheduler/kickmessage.go b/src/pkg/scheduler/kickmessage.go index 33ca9edac2..3a43c812c1 100644 --- a/src/pkg/scheduler/kickmessage.go +++ b/src/pkg/scheduler/kickmessage.go @@ -714,6 +714,7 @@ func (s *Scheduler) formatPRListWithPolicyForAgent(actionable *github.Actionable shown := fairShareByRepo(prs, limit, func(pr github.PullRequest) string { return pr.Repo }) verdicts := s.loadReviewVerdicts() links := loadReviewLinks() + busyCheck := s.busyBranchChecker(s.cfg.BaseAgentName(agentName)) for _, pr := range shown { // The PR title and author login are untrusted external text about to be // injected into an agent kick (F11). PR titles in particular drive @@ -733,7 +734,7 @@ func (s *Scheduler) formatPRListWithPolicyForAgent(actionable *github.Actionable if skip { continue } - annotationText = prKickAnnotation(pr, agentName) + reviewedAnnotation(verdicts, links, pr, s.cfg.Project.Org) + annotationText + annotationText = prKickAnnotation(pr, agentName) + reviewedAnnotation(verdicts, links, pr, s.cfg.Project.Org) + annotationText + busyBranchAnnotation(pr, s.cfg.BaseAgentName(agentName), busyCheck) annotation, annotationVerdict := s.enforceTextFrom(annotationText, src) failClosed = failClosed || (s.ioscanFailClosed() && annotationVerdict.HasCriticalInjection()) b.WriteString(fmt.Sprintf(" %s#%d by @%s%s %s %s\n", pr.Repo, pr.Number, author, forkAnnotation(pr), annotation, title)) diff --git a/src/pkg/scheduler/lane_messages.go b/src/pkg/scheduler/lane_messages.go index 4487e3d49e..37c45b9a39 100644 --- a/src/pkg/scheduler/lane_messages.go +++ b/src/pkg/scheduler/lane_messages.go @@ -517,28 +517,41 @@ func formatMergeEligibleDataFor(data []byte, keep func(repo string) bool, limit } func (s *Scheduler) buildCIFailingListFor(keep func(repo string) bool) string { + return s.buildCIFailingListForAgent("", keep) +} + +// buildCIFailingListForAgent is buildCIFailingListFor for one kicked agent: +// its own claims do not make a branch busy (busy_branch.go, +// hivecommons/hive#18488). An empty agent counts every claim. +func (s *Scheduler) buildCIFailingListForAgent(agent string, keep func(repo string) bool) string { data, err := os.ReadFile(ciFailingPath) if err != nil { return "(none)\n" } type ciFailingRow struct { - Number int `json:"number"` - Repo string `json:"repo"` - Title string `json:"title"` - Author string `json:"author"` - HeadSHA string `json:"head_sha"` - HeadRef string `json:"head_ref"` - HeadRepo string `json:"head_repo"` - FromFork bool `json:"from_fork"` - ReachableAction string `json:"reachable_action"` - Adopted bool `json:"adopted"` - SentinelAlert bool `json:"sentinel_alert"` - Held bool `json:"held"` - FailingChecks []string `json:"failing_checks"` - Excerpt string `json:"excerpt"` - DeferredIncident int `json:"deferred_incident"` - BaseRedChecks []string `json:"base_red_checks"` - BaseRed bool `json:"base_red"` + Number int `json:"number"` + Repo string `json:"repo"` + Title string `json:"title"` + Author string `json:"author"` + HeadSHA string `json:"head_sha"` + HeadRef string `json:"head_ref"` + HeadRepo string `json:"head_repo"` + FromFork bool `json:"from_fork"` + ReachableAction string `json:"reachable_action"` + Adopted bool `json:"adopted"` + SentinelAlert bool `json:"sentinel_alert"` + Held bool `json:"held"` + FailingChecks []string `json:"failing_checks"` + Excerpt string `json:"excerpt"` + DeferredIncident int `json:"deferred_incident"` + BaseRedChecks []string `json:"base_red_checks"` + BaseRed bool `json:"base_red"` + AppAuthored bool `json:"app_authored"` + HeadPushedAt *time.Time `json:"head_pushed_at"` + Issues []int `json:"issues"` + Agent string `json:"agent"` + TrustedAuthor bool `json:"trusted_author"` + ReroutedFrom string `json:"rerouted_from"` } var payload struct { Items []ciFailingRow `json:"ci_failing"` @@ -605,11 +618,17 @@ func (s *Scheduler) buildCIFailingListFor(keep func(repo string) bool) string { // instruction, and the CI-Repair step 4 "push a fixup commit to the PR // branch" is never aimed at a branch that only exists in someone's fork. var pushable, forks []ciFailingRow + var busy []busyRedPR + busyCheck := s.busyBranchChecker(agent) for _, pr := range payload.Items { if pr.FromFork && (!pr.Adopted || pr.ReachableAction != "push") { forks = append(forks, pr) } else if pr.ReachableAction == "comment-only" { forks = append(forks, pr) + } else if why := busyCheck(busyBranchRow{Repo: pr.Repo, Number: pr.Number, Issues: pr.Issues, AppAuthored: pr.AppAuthored, Adopted: pr.Adopted, HeadPushedAt: pr.HeadPushedAt, Own: ownRepairRow(pr.Agent, agent, pr.TrustedAuthor, pr.ReroutedFrom)}); why != "" { + // hivecommons/hive#18488: pushable, but someone else is on + // the branch right now. + busy = append(busy, busyRedPR{Number: pr.Number, Repo: pr.Repo, Title: pr.Title, FailingChecks: pr.FailingChecks, Why: why}) } else { pushable = append(pushable, pr) } @@ -646,6 +665,21 @@ func (s *Scheduler) buildCIFailingListFor(keep func(repo string) bool) string { b.WriteString(" " + adoptedSentinelNote + "\n") } } + if len(busy) > 0 { + b.WriteString(fmt.Sprintf("BUSY BRANCHES (%d — another worker is on these branches; suggest the fix, do not push):\n", len(busy))) + b.WriteString(" " + busyBranchInstruction + "\n") + for i, pr := range busy { + if i >= limit { + b.WriteString(prListOverflowLine(len(busy)-i, limit)) + break + } + checks := "" + if len(pr.FailingChecks) > 0 { + checks = " — failing: " + strings.Join(pr.FailingChecks, ", ") + } + b.WriteString(fmt.Sprintf(" #%d %s [busy: %s — suggest, do not push] — %s%s\n", pr.Number, pr.Repo, pr.Why, pr.Title, checks)) + } + } if len(deferred) > 0 { b.WriteString(fmt.Sprintf("DEFERRED TO OPEN SHARED-CI INCIDENTS (%d — no repair: the base/infra failure is tracked on the incident issue; do not push, retry, or comment again; each PR returns to the list above when its incident closes):\n", len(deferred))) for i, pr := range deferred { diff --git a/src/pkg/scheduler/policy_overlays.go b/src/pkg/scheduler/policy_overlays.go index 21b9dbb712..d27ebf3066 100644 --- a/src/pkg/scheduler/policy_overlays.go +++ b/src/pkg/scheduler/policy_overlays.go @@ -6,6 +6,7 @@ import ( "os" "slices" "strings" + "time" "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/review" @@ -205,7 +206,7 @@ func (s *Scheduler) redPRFixSection(agentName string) string { if err != nil { data = nil } - return formatRedPRFixDataWithSignals(data, base, github.CIFailureSignals().ForAgent(base)) + return formatRedPRFixDataGuarded(data, base, github.CIFailureSignals().ForAgent(base), s.busyBranchChecker(base)) } // CIFailureKickMessage is the targeted kick a webhook CI-failure signal @@ -283,30 +284,41 @@ func ciFailureSignalEvidence(sig github.CIFailureSignal) string { // own row. A signal whose check the file reports red on the base branch is // dropped like any other base-red failure. func formatRedPRFixDataWithSignals(data []byte, agent string, signals []github.CIFailureSignal) string { + return formatRedPRFixDataGuarded(data, agent, signals, nil) +} + +// formatRedPRFixDataGuarded is formatRedPRFixDataWithSignals with the +// busy-branch guard (busy_branch.go, hivecommons/hive#18488): a row the guard +// reports busy is not one of the agent's push targets; it is listed below the +// gate as "suggest, do not push" and does not hold back new work. +func formatRedPRFixDataGuarded(data []byte, agent string, signals []github.CIFailureSignal, busy busyBranchCheck) string { type ciFailingRow struct { - Number int `json:"number"` - Repo string `json:"repo"` - Title string `json:"title"` - Agent string `json:"agent"` - FailingChecks []string `json:"failing_checks"` - Excerpt string `json:"excerpt"` - Escalated bool `json:"escalated"` - FromFork bool `json:"from_fork"` - ReachableAction string `json:"reachable_action"` - Adopted bool `json:"adopted"` - SentinelAlert bool `json:"sentinel_alert"` - HeadRepo string `json:"head_repo"` - Held bool `json:"held"` - MergeableState string `json:"mergeable_state"` - Conflict bool `json:"conflict"` - ReroutedFrom string `json:"rerouted_from"` - DeferredIncident int `json:"deferred_incident"` - BaseRedChecks []string `json:"base_red_checks"` - BaseRed bool `json:"base_red"` - RebaseAttempt int `json:"rebase_attempt"` - RebaseMax int `json:"rebase_attempts_max"` - BaseRef string `json:"base_ref"` - TrustedAuthor bool `json:"trusted_author"` + Number int `json:"number"` + Repo string `json:"repo"` + Title string `json:"title"` + Agent string `json:"agent"` + FailingChecks []string `json:"failing_checks"` + Excerpt string `json:"excerpt"` + Escalated bool `json:"escalated"` + FromFork bool `json:"from_fork"` + ReachableAction string `json:"reachable_action"` + Adopted bool `json:"adopted"` + SentinelAlert bool `json:"sentinel_alert"` + HeadRepo string `json:"head_repo"` + Held bool `json:"held"` + MergeableState string `json:"mergeable_state"` + Conflict bool `json:"conflict"` + ReroutedFrom string `json:"rerouted_from"` + DeferredIncident int `json:"deferred_incident"` + BaseRedChecks []string `json:"base_red_checks"` + BaseRed bool `json:"base_red"` + RebaseAttempt int `json:"rebase_attempt"` + RebaseMax int `json:"rebase_attempts_max"` + BaseRef string `json:"base_ref"` + TrustedAuthor bool `json:"trusted_author"` + AppAuthored bool `json:"app_authored"` + HeadPushedAt *time.Time `json:"head_pushed_at"` + Issues []int `json:"issues"` } var payload struct { Items []ciFailingRow `json:"ci_failing"` @@ -353,6 +365,11 @@ func formatRedPRFixDataWithSignals(data []byte, agent string, signals []github.C } } var mine, deferred, baseRed []ciFailingRow + type busyRow struct { + row ciFailingRow + why string + } + var busyRows []busyRow forks := 0 for _, pr := range payload.Items { if pr.Escalated { @@ -391,13 +408,31 @@ func formatRedPRFixDataWithSignals(data []byte, agent string, signals []github.C baseRed = append(baseRed, pr) continue } + if busy != nil { + if why := busy(busyBranchRow{Repo: pr.Repo, Number: pr.Number, Issues: pr.Issues, AppAuthored: pr.AppAuthored, Adopted: pr.Adopted, HeadPushedAt: pr.HeadPushedAt, Own: ownRepairRow(pr.Agent, agent, pr.TrustedAuthor, pr.ReroutedFrom)}); why != "" { + busyRows = append(busyRows, busyRow{row: pr, why: why}) + continue + } + } mine = append(mine, pr) } - if len(mine) == 0 { + if len(mine) == 0 && len(busyRows) == 0 { return "" } var b strings.Builder + // Busy rows go after the gate (or alone): they are not push targets + // and must not hold back new work. + writeBusy := func() { + writeBusyRedPRs(&b, len(busyRows), func(i int) busyRedPR { + r := busyRows[i] + return busyRedPR{Number: r.row.Number, Repo: r.row.Repo, Title: r.row.Title, FailingChecks: r.row.FailingChecks, Excerpt: r.row.Excerpt, Why: r.why} + }) + } + if len(mine) == 0 { + writeBusy() + return b.String() + } b.WriteString(fmt.Sprintf("\n## 🔴 FIX-BEFORE-NEW — your open PRs with failing CI or merge conflicts (%d)\n\n", len(mine))) if forks > 0 { b.WriteString(fmt.Sprintf("(%d red PR(s) from forks are NOT listed here: you cannot push to a fork — they appear under CI_FAILING as comment-only.)\n", forks)) @@ -461,6 +496,7 @@ func formatRedPRFixDataWithSignals(data []byte, agent string, signals []github.C } } b.WriteString("\n") + writeBusy() return b.String() } diff --git a/src/pkg/scheduler/scheduler.go b/src/pkg/scheduler/scheduler.go index 6bf62d3a8d..a6531b4c37 100644 --- a/src/pkg/scheduler/scheduler.go +++ b/src/pkg/scheduler/scheduler.go @@ -44,6 +44,7 @@ type Scheduler struct { // logged, so it is logged once per day. classifierQuotaWarnedDay string inflight InflightLookup + prLease PRLeaseLookup maintainerNotes MaintainerNotesLookup runAdmitter RunAdmitter triageCommenter TriageCommenter diff --git a/src/pkg/scheduler/templates.go b/src/pkg/scheduler/templates.go index 1d3ccefd11..83dc7af76c 100644 --- a/src/pkg/scheduler/templates.go +++ b/src/pkg/scheduler/templates.go @@ -375,7 +375,7 @@ func (s *Scheduler) substituteTemplateWithVars(template string, actionable *gith // must not be handed a red PR on a repo it cannot push to (#6204). repoInScope := func(repo string) bool { return s.cfg.AgentServesRepo(agentName, repo) } mergeEligibleList := s.buildMergeEligibleListFor(repoInScope) - ciFailingList := s.buildCIFailingListFor(repoInScope) + ciFailingList := s.buildCIFailingListForAgent(baseName, repoInScope) // The built-in per-kick variables. Each value is already computed above, so // the thunks just return it — but wrapping them as resolve.RuntimeContext