diff --git a/changelog.d/fixed-17566-observe-only-auto-merge-off-lanes.md b/changelog.d/fixed-17566-observe-only-auto-merge-off-lanes.md new file mode 100644 index 0000000000..d6c02b8891 --- /dev/null +++ b/changelog.d/fixed-17566-observe-only-auto-merge-off-lanes.md @@ -0,0 +1 @@ +- Merge lanes: pull requests on a repo an owner switched off with `auto_merge: false` at L6 now get a stage record every pass (090 "auto-merge off" once approved and green) from an observe-only pass that never merges, enqueues, updates a branch, rebases or otherwise writes to GitHub; hives below L6 are unchanged. diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 0cd267d0cc..4ba58067fd 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -2371,6 +2371,7 @@ func (b *boot) bootAgentsWith(deps bootAgentsDeps) { autoMergeOpts.SelfAuthorizationHoldEnabled = selfAuthorizationHoldEnabled autoMergeOpts.MinHeadAge = b.cfg.AutoMerge.EffectiveMinHeadAge() autoMergeOpts.RepoAutoMergeEnabled = func(repo string) bool { return b.cfg.RepoAutoMergeEnabled(repo) } + autoMergeOpts.RepoAutoMergeSwitchedOff = func(repo string) bool { return b.cfg.RepoAutoMergeSwitchedOff(repo) } autoMergeOpts.RepoOnGitHub = func(repo string) bool { return b.cfg.RepoOnGitHub(repo) } // Read through b.cfg on every sweep tick so a config reload of // auto_merge.trusted_bot_authors takes effect without a restart. diff --git a/src/cmd/hive/main_sweeps.go b/src/cmd/hive/main_sweeps.go index b90713e512..855138f18c 100644 --- a/src/cmd/hive/main_sweeps.go +++ b/src/cmd/hive/main_sweeps.go @@ -907,6 +907,7 @@ func runAutoMergeSweepIfDue(ctx context.Context, ghClient *github.Client, cfg *c opts.RequiredChecksForRepo = cfg.AutoMerge.RequiredCheckSetForRepo opts.MinHeadAge = cfg.AutoMerge.EffectiveMinHeadAge() opts.RepoAutoMergeEnabled = func(repo string) bool { return cfg.RepoAutoMergeEnabled(repo) } + opts.RepoAutoMergeSwitchedOff = func(repo string) bool { return cfg.RepoAutoMergeSwitchedOff(repo) } opts.QueueOnApproveAndUnhold = func(repo string) bool { return cfg.AutoMerge.QueueOnApproveAndUnholdAllowed(repo, cfg.ACMMLevel) } diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 50b426a0a2..6610209cef 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -35,7 +35,9 @@ here. A repo with that switch off may still receive Hive-opened PRs, but the relay refuses to merge them and reports that auto-merge is disabled for the repository. The same repo policy is enforced by the App self-merge sweep and by the proxy's direct REST/GraphQL merge denial path, so agents cannot bypass it -by calling GitHub directly. +by calling GitHub directly. The merge lanes still evaluate such a repo's PRs +in an observe-only mode that records a stage (090 *auto-merge off*) and writes +nothing else. On top of `AuthorizeMerge`, the watcher's authorizer is wrapped with a second, target-specific check (the F4 target-binding, CWE-863) before any merge is diff --git a/src/docs/labels-and-control-signals/reference.md b/src/docs/labels-and-control-signals/reference.md index 4f97ca1026..72e287809b 100644 --- a/src/docs/labels-and-control-signals/reference.md +++ b/src/docs/labels-and-control-signals/reference.md @@ -88,7 +88,7 @@ Some hive-authored labels look like provenance but are read as signals, so they | `hold` from #5117 self-authorization | PRs | PR-request watcher and self-authorization release | A PR whose only rationale is unacknowledged hive-filed issues gets literal `hold`. Acknowledging the issue later does not remove an existing PR hold by itself. At ACMM L6 (Autonomous), the unset default is off; lower levels default on. | Hard merge gate | Hive App | PR creation; evaluated only when no level hold applies | Acknowledge the issue and remove the PR `hold`; disabled policy release can remove Hive's own hold. At L6, that release path also clears existing Hive-applied #5117 holds when no explicit config keeps the policy on. | `github.self_authorization_hold`, per-repo `project.repo_policies[].self_authorization_hold`, `HIVE_SELF_AUTHORIZATION_HOLD` (`src/pkg/github/pr_self_authorization.go:14-226`, `src/pkg/github/pr_request_watcher.go:452-524`) | | `triage/accepted` (or `project.issue_filter.reporter_trust.untrusted_require_labels`); `needs-triage` while waiting | Human-filed issues from untrusted reporters | GitHub enumeration | With `reporter_trust.enabled: true`, an issue whose reporter's GitHub `author_association` is outside the trusted set (default `OWNER`, `MEMBER`, `COLLABORATOR`) and whose login is not in `trusted_logins` is not actionable until it carries one of these labels. On the first excluded scan, core `github.Client.fetchIssues` posts one marked explanation comment and applies `needs-triage` (or `awaiting_label`); later scans do not repeat the comment. This is a poller-side write, not an agent prompt, and is capped at 10 new notices per poll. When the triage label appears, the same poll loop removes `needs-triage` only if the marker records that Hive added it, then the issue enters the backlog. Independently of the marker, the stage sweep removes `needs-triage` once the issue reaches 020 or later (claiming settles triage; see `stage/015-triage-needed` below) — this is what retires the `needs-triage` that `fleetreport` stamps on hive-filed self-reports at filing. Trusted reporters' issues pass untouched. Runs after holds/exempts and before `require_labels`, so the ordinary allow-list still applies afterwards. Hive- and bot-filed issues are not judged here (#5117 owns them). Counted as "needs triage" on the repo card, not as a generic filter refusal. | Hard admission gate (off by default) | Human; Hive App for the visibility comment/label | Enumeration | Add the label, trust the login or association under **Settings → Labels → Reporter trust**, or disable the gate | `project.issue_filter.reporter_trust.{enabled,trusted_associations,trusted_logins,untrusted_require_labels,awaiting_label,comment}` (`src/pkg/config/reporter_trust.go`, `src/pkg/github/client.go` `fetchIssues`) | | `hold` from #9665 reporter trust | PRs **not authored by the hive** | PR-request watcher, level-hold release | A PR whose rationale traces to an issue filed by an untrusted reporter gets literal `hold` at **every** ACMM level, including L6, plus `needs-human` (so it surfaces as waiting on a person, #10773) and a marked notice (`` + ``) naming the untrusted reporter and stating that the hold concerns the issue reporter, not the PR author. The rationale is **only the linked issues**: closing-keyword links (`Fixes`/`Closes`/`Resolves`) and the request's declared issues — bare `#N` mentions are not consulted, and a link that resolves to a pull request is skipped rather than judged (hivecommons/hive#15020: `Refs #15009 (closed by #15017)` made the gate judge sibling PR #15017 and attribute a `CONTRIBUTOR` who had merely commented). A hive-filed issue (`[selfreport]`, app or AI author) counts as trusted. The gate is **skipped entirely for hive-authored PRs** (App bot, `project.ai_author`, `author/hive`): the hive is the author, so the reporter's trust says nothing about whether its own fix may merge; it still applies to contributor PRs and to PRs opened under a shared PAT account. Any one untrusted citation holds. Evaluated even at hold-gated levels, because the notice is what stops promotion to L6 from releasing the label. | Hard merge gate | Hive App | PR creation; re-checked by the level-hold release path | A human removes `hold` on a contributor PR; Hive never auto-releases it there. On a **hive-authored** PR still wearing a hive-set reporter-trust hold (newest `hold` event labeled by the App bot with the notice at or after it — a person's hold is left alone), the stage-label pass releases `hold` (and `needs-human` when the notice says the gate added it), posts one `` comment and logs `stage labels: releasing self-imposed reporter-trust hold`. The pass also records the hold's provenance (`by`, `source`, `reason`, `since`) on the stage store, so the tracker pill reads *Held by hive · reporter trust (issue #N by @X, contributor)* and the 085 comment names who set the hold and why. Holdguard re-holds on a new head SHA. | `github.reporter_trust_hold` (nil follows `reporter_trust.enabled`), per-repo `project.repo_policies[].reporter_trust_hold`, `HIVE_REPORTER_TRUST_HOLD` (`src/pkg/github/pr_reporter_trust.go`, `src/pkg/github/pr_level_hold.go`, `src/pkg/github/automerge/reporter_trust_release.go`) | -| `project.repo_policies[].auto_merge: false` | PRs | `hive-merge` relay, App self-authored auto-merge sweep, proxy | Below L6 this resolves off for every repo; at L6, PRs can still be opened, reviewed and held when the per-repo switch is off, but Hive refuses every merge path for that repo. Existing `hold` / `hive-pause/` labels stay put because the disabled repo is skipped before hold release or merge. | Hard merge gate | Operator/dashboard | Merge relay request, self-merge sweep tick, proxy direct merge attempt | Dashboard repo-card switch or config edit sets `auto_merge: true`/removes the override at L6; enabling is rejected below L6 | `project.repo_policies[].auto_merge` (`src/pkg/config/repo_policy.go`, `src/crux/agent/manager_modes.go`, `src/pkg/github/automerge/automerge_sweep.go`, `src/pkg/proxy/rules.go`) | +| `project.repo_policies[].auto_merge: false` | PRs | `hive-merge` relay, App self-authored auto-merge sweep, proxy | Below L6 this resolves off for every repo; at L6, PRs can still be opened, reviewed and held when the per-repo switch is off, but Hive refuses every merge path for that repo. At L6 the merge lanes still evaluate a switched-off repo's PRs each pass in an observe-only mode that records a stage (090 *auto-merge off* once approved and green) and writes nothing else, so existing `hold` / `hive-pause/` labels stay put; below L6 the lanes skip the repo and stage nothing. | Hard merge gate | Operator/dashboard | Merge relay request, self-merge sweep tick, proxy direct merge attempt | Dashboard repo-card switch or config edit sets `auto_merge: true`/removes the override at L6; enabling is rejected below L6 | `project.repo_policies[].auto_merge` (`src/pkg/config/repo_policy.go`, `src/crux/agent/manager_modes.go`, `src/pkg/github/automerge/automerge_sweep.go`, `src/pkg/proxy/rules.go`) | | `hold` from holdguard | PRs | Holdguard ledger | If the head SHA changes while held, lifting the hold causes Hive to comment and re-apply literal `hold`; the next human removal is the fresh approval. Exception (#13603): when a human (not a Hive account, not a bot) removed the gate label after the current head arrived on the branch, they reviewed the current head, so the lift clears without a comment or re-hold. The head is accepted only on evidence a contributor cannot forge: the server-stamped `head_ref_force_pushed` timeline event that produced it (dated before the lift), or the hive itself having observed that exact head on the PR while the gate was still on (`observed_held_head_sha` in the holdguard ledger). Commit committer dates and SHA-keyed `check_suite` / `status` times are never used; a push after the lift, or a head the hive never saw while held, still re-holds. | Hard merge gate | Hive | Governor holdguard pass | Human removes the re-applied hold | Built-in `holdguard.ReHoldLabel` (`src/pkg/holdguard/holdguard.go:1-43`) | | `hold` from SHA-hold | Issues | SHA-hold sweep | Human-filed primary-repo `kind/bug` without a 7-40 hex SHA gets `hold` and a notice carrying ``; once a SHA appears in body/comments, Hive removes `hold` only if the current hold is its own (newest `hold` event labeled by the App bot, paired with an App-authored notice at or after it). A human's hold, a re-hold, or another subsystem's hold stays. Reporters whose author association is trusted (`issue_filter.reporter_trust.trusted_associations`, default OWNER/MEMBER/COLLABORATOR) get the soft `needs-sha` row below instead, and a SHA-hold `hold` already on such an issue is swapped for it (#12002). The hard-suppress reconcile also lifts this `hold` once a PR referencing the issue merges after it was applied. | Hard issue hold | Hive | Eval tick SHA sweep; hard-suppress reconcile | Add SHA evidence or merge the fix; sweep removes its own `hold`, a human removes any other | Primary repo and SHA-hold config (`src/pkg/github/client.go` `EnforceSHAHold`, `src/pkg/github/hard_suppress_reconcile.go`) | | `needs-sha` from SHA-hold | Issues | SHA-hold sweep | Trusted-reporter `kind/bug` without a SHA gets this soft label plus the same `` notice instead of `hold`; the issue stays actionable. The sweep removes it once a SHA appears. | Soft (informational) | Hive | Eval tick SHA sweep | Add SHA evidence, or remove the label | `issues.sha_hold_trusted_label` (default `needs-sha`; `hold` restores the hard hold for everyone) | @@ -807,7 +807,7 @@ On by default (`good_first_issue.enabled: false` opts out), hive applies both la **Applies to:** PRs. **Kind:** Hard merge gate. -Below L6 this resolves off for every repo; at L6, PRs can still be opened, reviewed and held when the per-repo switch is off, but Hive refuses every merge path for that repo. Existing `hold` / `hive-pause/` labels stay put because the disabled repo is skipped before hold release or merge. +Below L6 this resolves off for every repo; at L6, PRs can still be opened, reviewed and held when the per-repo switch is off, but Hive refuses every merge path for that repo. At L6 the merge lanes still evaluate a switched-off repo's PRs each pass in an observe-only mode that records a stage (090 *auto-merge off* once approved and green) and writes nothing else, so existing `hold` / `hive-pause/` labels stay put; below L6 the lanes skip the repo and stage nothing. **Who applies it:** Operator/dashboard. diff --git a/src/pkg/config/config_extra_test.go b/src/pkg/config/config_extra_test.go index 7de1d8bb3a..546eae59d1 100644 --- a/src/pkg/config/config_extra_test.go +++ b/src/pkg/config/config_extra_test.go @@ -894,6 +894,33 @@ func TestRepoAutoMergeEnabledFailsClosedBelowL6(t *testing.T) { } } +func TestRepoAutoMergeSwitchedOffIsL6ExplicitOffOnly(t *testing.T) { + f := false + tr := true + level := SelfMergeMinACMMLevel + cfg := &Config{ACMMLevel: &level, Project: ProjectConfig{ + Org: "acme", + RepoPolicies: []RepoPolicy{ + {Repo: "api", AutoMerge: &f}, + {Repo: "web", AutoMerge: &tr}, + }, + }} + if !cfg.RepoAutoMergeSwitchedOff("api") || !cfg.RepoAutoMergeSwitchedOff("ACME/API") { + t.Fatal("auto_merge=false at L6 is switched off") + } + if cfg.RepoAutoMergeSwitchedOff("acme/web") || cfg.RepoAutoMergeSwitchedOff("other") { + t.Fatal("auto_merge=true or no override is not switched off") + } + below := SelfMergeMinACMMLevel - 1 + cfg.ACMMLevel = &below + if cfg.RepoAutoMergeEnabled("api") || cfg.RepoAutoMergeSwitchedOff("api") { + t.Fatal("below L6 auto-merge is off everywhere but no repo counts as switched off") + } + if (*Config)(nil).RepoAutoMergeSwitchedOff("acme/api") { + t.Fatal("nil config is not switched off") + } +} + func TestSetSelfAuthorizationHoldForRepos(t *testing.T) { cfg := &Config{ SourcePath: writeTempConfig(t, "project:\n org: acme\n repos: [api]\ngithub:\n token: ghp_tok\nagents:\n bot:\n backend: claude\n"), diff --git a/src/pkg/config/repo_policy.go b/src/pkg/config/repo_policy.go index ab70209a1b..54f75e3218 100644 --- a/src/pkg/config/repo_policy.go +++ b/src/pkg/config/repo_policy.go @@ -787,6 +787,19 @@ func (c *Config) RepoAutoMergeEnabled(repo string) bool { return true } +// RepoAutoMergeSwitchedOff reports whether an owner switched this repo's +// auto-merge off with `project.repo_policies[].auto_merge: false` at L6 or +// above. It is the observe-only staging predicate (hivecommons/hive#17552): +// below L6 RepoAutoMergeEnabled is false for every repo, but those hives +// are not staged, so this is narrower than !RepoAutoMergeEnabled. +func (c *Config) RepoAutoMergeSwitchedOff(repo string) bool { + if c == nil || c.ACMMLevelOrZero() < SelfMergeMinACMMLevel { + return false + } + rp, ok := c.RepoPolicyFor(repo) + return ok && rp.AutoMerge != nil && !*rp.AutoMerge +} + // SetRepoAutoMergeForRepoAndSave records, clears, and persists one repo's // auto-merge override. nil clears the override so the repo inherits default ON. func (c *Config) SetRepoAutoMergeForRepoAndSave(repo string, enabled *bool) (bool, error) { diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 0016d0075f..d46935ebaa 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -428,7 +428,14 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe continue } if !c.repoAutoMergeAllowed(displayRepo) { - c.info("contributor-lane automerge sweep skipped repo", "repo", displayRepo, "reason", "repo-auto-merge-disabled") + c.info("contributor-lane automerge sweep skipped repo", "repo", displayRepo, "reason", "repo-auto-merge-disabled", "observe_only", c.stagePolicy().Enabled && c.repoObservedOnly(displayRepo)) + c.observeAutoMergeOffRepo(ctx, stageLaneContributor, displayRepo, opts.Targets, func(ctx context.Context, oc *Engine, owner, repoName string, pr *gh.PullRequest) (string, error) { + if reason := oc.sentinelBlockedUnlessHumanChangesRequested(ctx, owner, repoName, pr, oc.prefilterContributorLanePR(pr, policy)); reason != "" { + return reason, nil + } + _, reason, err := oc.trySweepContributorLanePR(mergeDecisionPriority(ctx), displayRepo, owner, repoName, pr.GetNumber(), policy) + return reason, err + }) continue } prs, skipReason, err := c.openPullRequests(ctx, owner, repoName, opts.Targets) diff --git a/src/pkg/github/automerge/automerge_merge_train.go b/src/pkg/github/automerge/automerge_merge_train.go index 1323bf2ecc..303bd9fdc8 100644 --- a/src/pkg/github/automerge/automerge_merge_train.go +++ b/src/pkg/github/automerge/automerge_merge_train.go @@ -527,7 +527,7 @@ func mergeTrainCandidate(prefilterReason string) bool { // train, so a failed read never blocks a merge the lane would have made. func (c *Engine) planMergeTrains(ctx context.Context, lane, displayRepo, owner, repo string, prs []*gh.PullRequest, prefilter func(*gh.PullRequest) string) *mergeTrainPlan { plan := &mergeTrainPlan{lane: lane, displayRepo: displayRepo, owner: owner, repo: repo, byNumber: map[int]*mergeTrainGroup{}, candidates: map[int]*mergeTrainGroup{}} - if !c.mergeTrainPolicyNow().Enabled || c == nil || c.gh == nil { + if !c.mergeTrainPolicyNow().Enabled || c == nil || c.gh == nil || c.observeOnly { return plan } defer c.mergeTrainSettleSeats(lane, owner, repo, displayRepo, prs, plan) diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go index add613b5dd..3fb7055992 100644 --- a/src/pkg/github/automerge/automerge_native_queue.go +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -225,20 +225,30 @@ func (c *Engine) nativeQueue(repo string) nativeQueueTransport { return c.nativeQueueTransport() } +const ( + integrationMemberCaptureUnavailable = "integration exact member capture unavailable" + integrationMemberIdentityChanged = "integration exact member identity changed" +) + // routeIntegrationMerge applies the exact-member capture and execution // authority before either serialized or direct merge execution. func (c *Engine) routeIntegrationMerge(ctx context.Context, pr *gh.PullRequest, claim effects.Claim) (bool, string, error) { if capture, ok := ctx.Value(integrationEligibilityContextKey{}).(*integrationEligibilityCapture); ok { if capture == nil { - return false, "integration exact member capture unavailable", nil + return false, integrationMemberCaptureUnavailable, nil } if pr.GetNumber() != capture.number || pr.GetHead().GetSHA() != capture.head || pr.GetBase().GetRef() != capture.base || pr.GetBase().GetRepo().GetFullName() != capture.repo { - return false, "integration exact member identity changed", nil + return false, integrationMemberIdentityChanged, nil } capture.pr = pr return false, integrationEligibilityCaptured, nil } + if c.observeOnly { + // An observe-only pass stops at the merge decision: no merge, no + // native-queue enqueue (automerge_observe_only.go). + return false, observeOnlyReasonRepoAutoMergeDisabled, nil + } requiredNative := false if c.integrationExecution != nil { decision, err := c.integrationExecution(ctx, claim.Repo, pr, c.IntegrationEligibility) @@ -361,6 +371,9 @@ func (c *Engine) tryNativeQueueAfterRouting(ctx context.Context, pr *gh.PullRequ // reconcileNativeQueueGate runs for the owning lane's rejected candidates, // including cheap prefilter holds. Never withdraw another actor's entry. func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo string, pr *gh.PullRequest, reason string) { + if c == nil || c.observeOnly { + return + } t := c.nativeQueue(owner + "/" + repo) if t == nil || pr == nil || pr.GetNodeID() == "" || !nativeQueueWithdrawReason(reason) { return diff --git a/src/pkg/github/automerge/automerge_observe_only.go b/src/pkg/github/automerge/automerge_observe_only.go new file mode 100644 index 0000000000..04241b2b78 --- /dev/null +++ b/src/pkg/github/automerge/automerge_observe_only.go @@ -0,0 +1,109 @@ +package automerge + +import ( + "context" + "fmt" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/effects" + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// observeOnlyReasonRepoAutoMergeDisabled is the lane reason an observe-only +// pass records for a head that reached the merge decision on a repo whose +// auto-merge is off (hivecommons/hive#17566). The stage engine classifies it +// from the head's own evidence, and prstage.HumanMergeReason names its 090 +// "auto-merge off". +const observeOnlyReasonRepoAutoMergeDisabled = "repo-auto-merge-disabled" + +// observeOnlyBoundary refuses every effect an observe-only lane evaluation +// reaches: merges, branch updates, comments, label and hold changes. +type observeOnlyBoundary struct{} + +func (observeOnlyBoundary) Execute(_ context.Context, claim effects.Claim, _ func(context.Context) (effects.Result, error)) (effects.Result, error) { + return effects.Result{}, effects.NotApplied(fmt.Errorf("observe-only pass on a repo whose auto-merge is off cannot mutate %s", claim.Kind)) +} + +// observeOnlyEngine is an isolated engine for a lane's observe-only pass. It +// keeps the live policy callbacks and feeds this engine's stage tracker and +// store, but refuses every write: its mutation boundary rejects all effects, +// its observeOnly flag stops the merge, native-queue and merge-train helpers +// and the write-back gate, and its one-per-head marks stay private. +func (c *Engine) observeOnlyEngine() *Engine { + options := c.integrationReadOnlyOptions() + options.MutationBoundary = observeOnlyBoundary{} + options.StageTracker = c.stageTracker + options.observeOnly = true + return New(c.transport, options) +} + +// observeLaneEval runs one lane's per-PR evaluation of a listed PR on the +// observe-only engine, its prefilter included, and returns the reason the +// lane settled on. +type observeLaneEval func(ctx context.Context, oc *Engine, owner, repoName string, pr *gh.PullRequest) (string, error) + +// observeAutoMergeOffRepo is a lane's pass over a repo whose auto-merge is +// off. The lanes used to skip such a repo before recording anything, so its +// PRs never got a stage (hivecommons/hive#17552). Each open PR is evaluated +// as far as the merge decision and recorded for ApplyStageLabels; nothing is +// written to GitHub: the requests run under hgithub.WithIntegrationReadOnly, +// the merge is captured instead of executed, and there is no merge train, +// native-queue reconcile, hold release or workflow approval. A no-op when +// stage labelling is off, since the record is all the pass produces, and +// for a repo that is not one an owner switched off at L6 (repoObservedOnly): +// hives below L6 are not staged. +func (c *Engine) observeAutoMergeOffRepo(ctx context.Context, lane, displayRepo string, targets SweepTargets, eval observeLaneEval) { + if c == nil || !c.stagePolicy().Enabled || eval == nil || !c.repoObservedOnly(displayRepo) { + return + } + oc := c.observeOnlyEngine() + ctx = hgithub.WithIntegrationReadOnly(ctx) + owner, repoName := oc.transport.SplitRepo(displayRepo) + prs, skipReason, err := oc.openPullRequests(ctx, owner, repoName, targets) + if err != nil { + c.warn("observe-only automerge pass could not list open PRs", "lane", lane, "repo", displayRepo, "error", err) + return + } + if skipReason != "" { + c.debug("observe-only automerge pass skipped repo", "lane", lane, "repo", displayRepo, "reason", skipReason) + return + } + for _, pr := range prs { + if pr == nil || pr.GetNumber() <= 0 { + continue + } + base := pr.GetBase().GetRepo().GetFullName() + if base == "" { + base = owner + "/" + repoName + } + capture := &integrationEligibilityCapture{repo: base, base: pr.GetBase().GetRef(), head: pr.GetHead().GetSHA(), number: pr.GetNumber()} + // The capture also disarms the base-preparation and queued-sweep probes (integrationEligibilityProbe), so it is not redundant with observeOnly. + reason, err := eval(context.WithValue(ctx, integrationEligibilityContextKey{}, capture), oc, owner, repoName, pr) + if observed := observeOnlyStageReason(lane, reason); observed != reason { + // The lane got as far as the merge (or a step only a merge + // would take); its refusal is this pass's, not the head's. + reason, err = observed, nil + } + c.debug("observe-only automerge pass recorded PR", "lane", lane, "repo", displayRepo, "pr", pr.GetNumber(), "reason", reason, "error", err) + oc.recordStage(lane, displayRepo, owner, repoName, pr, reason, false, err) + } +} + +// observeOnlyStageReason is the reason an observe-only pass records for the +// lane's own: a head that reached the merge decision, or stopped at a step +// that only leads to a merge (the soak, the merge queue, a branch update), +// is recorded as repo-auto-merge-disabled, so it is never staged 080, 095 +// or 100 when nothing will merge it. Every other reason is the head's own +// evidence (red or pending CI, a conflict, a hold, an admission or hand-off +// reason) and is kept. +func observeOnlyStageReason(lane, reason string) string { + if reason == "" || reason == integrationEligibilityCaptured || reason == integrationMemberCaptureUnavailable || reason == integrationMemberIdentityChanged { + return observeOnlyReasonRepoAutoMergeDisabled + } + switch classifyStage(stageObservation{Lane: lane, Reason: reason}).Stage { + case stagelabels.AwaitingMergeAdmission, stagelabels.PreMergeSoak, stagelabels.MergeQueue: + return observeOnlyReasonRepoAutoMergeDisabled + } + return reason +} diff --git a/src/pkg/github/automerge/automerge_observe_only_test.go b/src/pkg/github/automerge/automerge_observe_only_test.go new file mode 100644 index 0000000000..ddb2622ad0 --- /dev/null +++ b/src/pkg/github/automerge/automerge_observe_only_test.go @@ -0,0 +1,243 @@ +package automerge + +import ( + "context" + "encoding/json" + "net/http" + "path/filepath" + "slices" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/effects" + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stageengine/prstage" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// observeOnlyLaneAPI serves one open, approved, green PR (#7 by alice, head +// sha7) on acme/widget to the merge lanes, on top of the stage API the +// stage writer uses, and records every request that is not a read. +type observeOnlyLaneAPI struct { + writes []string + merges int + updates int + graphql int +} + +func (a *observeOnlyLaneAPI) serve(w http.ResponseWriter, r *http.Request) bool { + if r.Method != http.MethodGet && r.Method != http.MethodHead { + a.writes = append(a.writes, r.Method+" "+r.URL.Path) + switch { + case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/merge": + a.merges++ + _ = json.NewEncoder(w).Encode(map[string]any{"merged": true, "sha": "merge7"}) + return true + case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/update-branch": + a.updates++ + _ = json.NewEncoder(w).Encode(map[string]any{"message": "Updating pull request branch."}) + return true + case r.URL.Path == "/graphql" || r.URL.Path == "/api/graphql": + a.graphql++ + _ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{}}) + return true + } + return false + } + if serveUnprotectedSweepBase(w, r) { + return true + } + pull := map[string]any{ + "number": 7, + "state": "open", + "mergeable_state": "clean", + "mergeable": true, + "user": map[string]string{"login": "alice"}, + "head": map[string]any{"sha": "sha7", "ref": "feature", "repo": map[string]string{"full_name": "acme/widget"}}, + "base": map[string]any{"ref": "main", "sha": "base1", "repo": map[string]string{"full_name": "acme/widget"}}, + "labels": []map[string]string{}, + } + switch r.URL.Path { + case "/repos/acme/widget/pulls": + _ = json.NewEncoder(w).Encode([]map[string]any{pull}) + case "/repos/acme/widget/pulls/7": + _ = json.NewEncoder(w).Encode(pull) + case "/repos/acme/widget/pulls/7/reviews": + _ = json.NewEncoder(w).Encode([]map[string]any{}) + case "/repos/acme/widget/commits/sha7/status": + _ = json.NewEncoder(w).Encode(map[string]any{ + "state": "success", + "total_count": 1, + "statuses": []map[string]string{{"context": "ci/build", "state": "success"}}, + }) + case "/repos/acme/widget/commits/sha7/check-runs": + _ = json.NewEncoder(w).Encode(map[string]any{ + "total_count": 1, + "check_runs": []map[string]string{{"name": "build", "status": "completed", "conclusion": "success"}}, + }) + default: + return false + } + return true +} + +// TestObserveOnlyPassStagesAutoMergeOffRepoWithoutWrites: on a repo whose +// auto-merge is off the lanes used to skip the repo before recording any +// stage evidence, so an approved, green PR there never got a stage +// (hivecommons/hive#17566). Each lane now observes it: the drain holds a +// repo-auto-merge-disabled observation, the stage writer parks it at 090 +// "auto-merge off", and the lanes send GitHub no write at all — no merge, +// no branch update, no native-queue enqueue. Only a repo an owner switched +// off at L6 is observed (hivecommons/hive#17552 decision A): on an ACMM L5 +// hive, where auto-merge is off everywhere and RepoAutoMergeSwitchedOff +// names no repo, the lanes skip the repo as before and stage nothing. +func TestObserveOnlyPassStagesAutoMergeOffRepoWithoutWrites(t *testing.T) { + for _, tc := range []struct { + name string + acmmAllowed bool // ACMM L6 (self-authored lane runs, repo switched off) vs L5 + }{ + {name: "acmm L6", acmmAllowed: true}, + {name: "acmm L5", acmmAllowed: false}, + } { + t.Run(tc.name, func(t *testing.T) { + lane := &observeOnlyLaneAPI{} + api := &stageAPI{labels: map[string]string{}, extra: lane.serve} + for _, d := range stagelabels.AllLabelDefs() { + api.labels[d.Name] = d.Color + } + srv := newStageAPI(t, api) + defer srv.Close() + setStagePRTruth(7) + + clock := &contributorLaneClock{at: time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC)} + client := hgithub.NewClient("token", "acme", []string{"widget"}, nil, srv.URL) + client.SetAppBotLogin(testHiveAppBotLogin) + tracker := NewStageTracker() + store := stagelabels.NewStore(filepath.Join(t.TempDir(), "stage.json"), clock.now) + e := New(client, Options{ + Now: clock.now, + StageLabelPolicy: func() StageLabelPolicy { return StageLabelPolicy{Enabled: true} }, + StageTracker: tracker, + StageStore: store, + CheckSummary: greenChecks, + BasePreparation: NewBasePreparationStore(""), + RepoAutoMergeEnabled: func(string) bool { return false }, + RepoAutoMergeSwitchedOff: func(repo string) bool { + // The lanes pass the transport's display name (here + // the short "widget"); config.RepoPolicyFor accepts both. + return tc.acmmAllowed && (strings.EqualFold(repo, "widget") || strings.EqualFold(repo, "acme/widget")) + }, + TrustedAuthorPolicy: func() TrustedAuthorPolicy { return TrustedAuthorPolicy{Enabled: true} }, + TrustedAuthorizer: func(login, requireRole string) TrustedAuthorDecision { + return TrustedAuthorDecision{Allowed: login == "alice", Role: requireRole} + }, + ContributorLaneVerdict: func(string, int, string) ContributorLaneVerdict { + return ContributorLaneVerdict{Configured: true, Approved: true, Verdict: "approve"} + }, + }) + ctx := context.Background() + + // The lanes sweepPass runs for this ACMM level. + if tc.acmmAllowed { + res, err := e.SweepSelfAuthoredAutoMerges(ctx, AutoMergeSweepOptions{}) + if err != nil || len(res.Merged) != 0 { + t.Fatalf("self-authored lane: result=%+v err=%v", res, err) + } + } + res, err := e.SweepTrustedAuthorAutoMerges(ctx, AutoMergeSweepOptions{SelfAuthoredLaneActive: tc.acmmAllowed}) + if err != nil || len(res.Merged) != 0 { + t.Fatalf("trusted-author lane: result=%+v err=%v", res, err) + } + if len(lane.writes) != 0 { + t.Fatalf("lanes wrote to GitHub on an auto-merge-off repo: %v", lane.writes) + } + + tracker.mu.Lock() + obs, ok := tracker.obs[stageKey("acme", "widget", 7)][stageLaneTrusted] + tracker.mu.Unlock() + if !tc.acmmAllowed { + if ok { + t.Fatalf("below L6 the lanes must not observe the repo, got %+v", obs) + } + if _, err := e.ApplyStageLabels(ctx); err != nil { + t.Fatalf("ApplyStageLabels: %v", err) + } + if rec, ok := store.Get(stageKey("acme", "widget", 7)); ok { + t.Fatalf("below L6 the PR must have no stage record, got %+v", rec) + } + if len(api.added) != 0 || lane.merges != 0 || lane.updates != 0 || lane.graphql != 0 { + t.Fatalf("below L6 the lanes wrote labels=%v merges=%d update-branch=%d graphql=%d, want none", api.added, lane.merges, lane.updates, lane.graphql) + } + return + } + if !ok { + t.Fatal("trusted-author lane recorded no observation for the auto-merge-off repo's PR") + } + if obs.Reason != observeOnlyReasonRepoAutoMergeDisabled || obs.Merged || obs.Err || obs.HeadSHA != "sha7" { + t.Fatalf("observation = reason %q merged %v err %v head %q, want %q at sha7", obs.Reason, obs.Merged, obs.Err, obs.HeadSHA, observeOnlyReasonRepoAutoMergeDisabled) + } + + if _, err := e.ApplyStageLabels(ctx); err != nil { + t.Fatalf("ApplyStageLabels: %v", err) + } + rec, ok := store.Get(stageKey("acme", "widget", 7)) + if !ok || rec.Stage != stagelabels.ReadyForHumanMerge { + t.Fatalf("stage record = %+v (ok %v), want %s", rec, ok, stagelabels.ReadyForHumanMerge) + } + if got := prstage.HumanMergeReason(rec.Reason); got != prstage.HumanMergeReasonAutoMergeOff { + t.Fatalf("human merge reason of %q = %q, want %q", rec.Reason, got, prstage.HumanMergeReasonAutoMergeOff) + } + if !slices.Contains(slices.Concat(api.added...), stagelabels.ReadyForHumanMerge.Label()) { + t.Fatalf("stage labels added = %v, want %s", api.added, stagelabels.ReadyForHumanMerge.Label()) + } + if lane.merges != 0 || lane.updates != 0 || lane.graphql != 0 { + t.Fatalf("merges=%d update-branch=%d graphql (enqueue)=%d, want none", lane.merges, lane.updates, lane.graphql) + } + }) + } +} + +// The observe-only pass keeps the head's own evidence and replaces only the +// reasons that would stage a PR nothing will merge as merging. +func TestObserveOnlyStageReason(t *testing.T) { + for reason, want := range map[string]string{ + "": observeOnlyReasonRepoAutoMergeDisabled, + integrationEligibilityCaptured: observeOnlyReasonRepoAutoMergeDisabled, + "behind": observeOnlyReasonRepoAutoMergeDisabled, + "update-branch": observeOnlyReasonRepoAutoMergeDisabled, + contributorLaneReasonSoaking: observeOnlyReasonRepoAutoMergeDisabled, + "conflicting": "conflicting", + "label:hold": "label:hold", + "draft": "draft", + "not-app-authored": "not-app-authored", + "untrusted-author-role": "untrusted-author-role", + "required-check-failing:build": "required-check-failing:build", + "check-pending": "check-pending", + } { + if got := observeOnlyStageReason(stageLaneTrusted, reason); got != want { + t.Errorf("observeOnlyStageReason(%q) = %q, want %q", reason, got, want) + } + } +} + +// The engine an observe-only pass runs on refuses every write path the +// lanes reach, whatever the live engine allows. +func TestObserveOnlyEngineRefusesWrites(t *testing.T) { + client := hgithub.NewClient("token", "acme", []string{"widget"}, nil, "http://127.0.0.1:1") + client.SetAppBotLogin(testHiveAppBotLogin) + e := New(client, Options{StageLabelPolicy: func() StageLabelPolicy { return StageLabelPolicy{Enabled: true} }}) + oc := e.observeOnlyEngine() + if e.observeOnly || !oc.observeOnly { + t.Fatalf("observeOnly live=%v observe=%v, want false/true", e.observeOnly, oc.observeOnly) + } + if oc.stageTrackerOrShared() != e.stageTrackerOrShared() { + t.Fatal("observe-only engine must record into the live engine's stage tracker") + } + if d := oc.writebackAllowed("acme/widget", writebackItemForObservation(stageObservation{Number: 7}), "", ""); d.Allowed { + t.Fatalf("write-back gate allowed a write in an observe-only pass: %+v", d) + } + if _, reason, err := oc.routeIntegrationMerge(context.Background(), nil, effects.Claim{Repo: "acme/widget", Kind: effects.KindPullRequestMerge}); reason != observeOnlyReasonRepoAutoMergeDisabled || err != nil { + t.Fatalf("routeIntegrationMerge = %q, %v; want the merge refused as %q", reason, err, observeOnlyReasonRepoAutoMergeDisabled) + } +} diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 978db6fdf4..6dd4b44160 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -979,8 +979,13 @@ func (c *Engine) stageAuthorKind(obs stageObservation, policy StageLabelPolicy, // on the next pass. Seen on the hive spoke for // every maintainer PR; the fix is to treat an incomplete drain as no // opinion rather than to trust the weak one. +// +// A repo an owner switched off at L6 is no exception: every lane observes +// it in an observe-only pass (automerge_observe_only.go), so the same lanes +// must have spoken before a weak opinion there is acted on. Any other repo +// the lanes skip has no lanes to wait for. func (c *Engine) missingStageLanes(displayRepo string, list []stageObservation) []string { - if c == nil || !c.repoAutoMergeAllowed(displayRepo) { + if c == nil || (!c.repoAutoMergeAllowed(displayRepo) && !c.repoObservedOnly(displayRepo)) { return nil } seen := make(map[string]bool, len(list)) diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 29254809f0..e0f6811fae 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -156,6 +156,12 @@ type Options struct { // RepoAutoMergeEnabled returns the live per-repo auto-merge switch. nil // preserves default-on behavior. RepoAutoMergeEnabled func(repo string) bool + // RepoAutoMergeSwitchedOff reports a repo an owner switched off with + // auto_merge: false at L6 (config.RepoAutoMergeSwitchedOff). Only such a + // repo gets the observe-only staging pass (hivecommons/hive#17552); + // below L6, where RepoAutoMergeEnabled is false everywhere, nothing is + // staged. nil means no repo is observed. + RepoAutoMergeSwitchedOff func(repo string) bool // QueueOnApproveAndUnhold returns the live per-repo // auto_merge.queue_on_approve_and_unhold opt-in at the hive's ACMM level // (hivecommons/hive#13629). repo is owner/repo. nil means off. @@ -303,6 +309,10 @@ type Options struct { // instead of the transport's shared one: a read-only evaluation must // not mark a head as tried (automerge_sweep_memory.go). isolatedSweepMemory bool + // observeOnly builds the engine for a lane's observe-only pass on a + // repo whose auto-merge is off (automerge_observe_only.go): it records + // stage evidence and refuses every write. + observeOnly bool } // Engine owns the automerge sweep policy state. @@ -396,6 +406,7 @@ type Engine struct { selfAuthorizationHoldEnabled func(repo string) bool selfAuthorizationHoldReleaseLimit int repoAutoMergeEnabled func(repo string) bool + repoAutoMergeSwitchedOff func(repo string) bool queueOnApproveAndUnhold func(repo string) bool repoOnGitHub func(repo string) bool trustedBotAuthors func() map[string]bool @@ -497,6 +508,8 @@ type Engine struct { fixDispatch func(repo string, number int, head string) (review.FixDispatchStatus, bool) hotspotRecorder HotspotRecorder integrationFilesObserved func(repo, head string, files []string) + // observeOnly: see Options.observeOnly. The write helpers check it. + observeOnly bool mergeTrainState } @@ -532,6 +545,7 @@ func New(transport Transport, opts Options) *Engine { selfAuthorizationHoldEnabled: opts.SelfAuthorizationHoldEnabled, selfAuthorizationHoldReleaseLimit: opts.SelfAuthorizationHoldReleaseLimit, repoAutoMergeEnabled: opts.RepoAutoMergeEnabled, + repoAutoMergeSwitchedOff: opts.RepoAutoMergeSwitchedOff, queueOnApproveAndUnhold: opts.QueueOnApproveAndUnhold, repoOnGitHub: opts.RepoOnGitHub, trustedBotAuthors: opts.TrustedBotAuthors, @@ -569,6 +583,7 @@ func New(transport Transport, opts Options) *Engine { fixDispatch: opts.FixDispatchStatus, hotspotRecorder: opts.HotspotRecorder, integrationFilesObserved: opts.IntegrationFilesObserved, + observeOnly: opts.observeOnly, evaluatedHeads: make(map[string]string), requiredChecksFallbackWarned: make(map[string]bool), } @@ -631,6 +646,16 @@ func (c *Engine) repoAutoMergeAllowed(repo string) bool { return c.repoAutoMergeEnabled(repo) } +// repoObservedOnly reports whether the lanes stage repo without merging on +// it: an owner switched its auto-merge off at L6. A repo auto-merge refuses +// for any other reason (below L6, bad merge_strategy) is skipped as before. +func (c *Engine) repoObservedOnly(repo string) bool { + if c == nil || c.repoAutoMergeSwitchedOff == nil || c.repoAutoMergeAllowed(repo) { + return false + } + return c.repoAutoMergeSwitchedOff(repo) +} + // activeRepos is Repositories() minus the repos under an operator pause // (#6203) and minus the repos that live on another forge (#14150). The pause // narrowing lives behind an optional transport capability, the same shape New diff --git a/src/pkg/github/automerge/automerge_sweep_loop.go b/src/pkg/github/automerge/automerge_sweep_loop.go index cd049cde8d..ea7cbfaea6 100644 --- a/src/pkg/github/automerge/automerge_sweep_loop.go +++ b/src/pkg/github/automerge/automerge_sweep_loop.go @@ -42,6 +42,9 @@ func (c *Engine) SweepQueuedAutoMerges(ctx context.Context, opts AutoMergeSweepO break } if !c.repoAutoMergeAllowed(repo) { + // No observe-only pass here: this sweep records no stage + // evidence on any repo. The self-authored, trusted-author and + // contributor lanes observe the same PRs (automerge_observe_only.go). c.info("automerge sweep skipped repo", "repo", repo, "reason", "repo-auto-merge-disabled") continue } @@ -181,7 +184,14 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge continue } if !c.repoAutoMergeAllowed(repo) { - c.info("self-authored automerge sweep skipped repo", "repo", repo, "reason", "repo-auto-merge-disabled") + c.info("self-authored automerge sweep skipped repo", "repo", repo, "reason", "repo-auto-merge-disabled", "observe_only", c.stagePolicy().Enabled && c.repoObservedOnly(repo)) + c.observeAutoMergeOffRepo(ctx, stageLaneSelf, repo, opts.Targets, func(ctx context.Context, oc *Engine, owner, repoName string, pr *gh.PullRequest) (string, error) { + if reason := oc.prefilterSelfAuthoredPR(pr); reason != "" { + return reason, nil + } + _, reason, err := oc.trySweepSelfAuthoredPR(mergeDecisionPriority(ctx), repo, owner, repoName, pr.GetNumber(), false) + return reason, err + }) continue } prs, skipReason, err := c.openPullRequests(ctx, owner, repoName, opts.Targets) @@ -432,7 +442,21 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg continue } if !c.repoAutoMergeAllowed(displayRepo) { - c.info("trusted-author automerge sweep skipped repo", "repo", displayRepo, "reason", "repo-auto-merge-disabled") + c.info("trusted-author automerge sweep skipped repo", "repo", displayRepo, "reason", "repo-auto-merge-disabled", "observe_only", c.stagePolicy().Enabled && c.repoObservedOnly(displayRepo)) + c.observeAutoMergeOffRepo(ctx, stageLaneTrusted, displayRepo, opts.Targets, func(ctx context.Context, oc *Engine, owner, repoName string, pr *gh.PullRequest) (string, error) { + login := hgithub.SafeGetLogin(pr.GetUser()) + if opts.SelfAuthoredLaneActive && oc.isTrustedBotLaneAuthor(login) { + return trustedBotLaneSkipReason, nil + } + if lane, _ := oc.sweepLaneForAuthor(login); lane == stageLaneSelf { + return trustedAuthorReasonAppAuthored, nil + } + if reason := oc.prefilterTrustedAuthorPR(owner, repoName, pr, policy); reason != "" { + return reason, nil + } + _, reason, err := oc.trySweepTrustedAuthorPR(mergeDecisionPriority(ctx), displayRepo, owner, repoName, pr.GetNumber(), policy) + return reason, err + }) continue } prs, skipReason, err := c.openPullRequests(ctx, owner, repoName, opts.Targets) diff --git a/src/pkg/github/automerge/automerge_writeback.go b/src/pkg/github/automerge/automerge_writeback.go index 8bb146a92a..2bec8ca5f3 100644 --- a/src/pkg/github/automerge/automerge_writeback.go +++ b/src/pkg/github/automerge/automerge_writeback.go @@ -29,6 +29,9 @@ func (c *Engine) writebackGate() *writeback.Gate { // engine). A refusal is recorded by the gate for the dashboard drill-down; // the caller skips the write. func (c *Engine) writebackAllowed(repo string, item writeback.Item, kind writeback.Kind, preview string) writeback.Decision { + if c != nil && c.observeOnly { + return writeback.Decision{Reason: observeOnlyReasonRepoAutoMergeDisabled, Detail: "observe-only pass on a repo whose auto-merge is off"} + } return c.writebackGate().AllowedPreview(repo, item, kind, preview) }