diff --git a/changelog.d/fixed-automerge-once-per-head-memory.md b/changelog.d/fixed-automerge-once-per-head-memory.md new file mode 100644 index 0000000000..442e1e4692 --- /dev/null +++ b/changelog.d/fixed-automerge-once-per-head-memory.md @@ -0,0 +1 @@ +- automerge: the lanes' "once per PR head" memory now lasts for the life of the hive process instead of one sweep. The queued, trusted-author and contributor lanes run every minute on a fresh engine, and the memory lived on that engine, so it was empty on every tick. A refused `update-branch` (a fork head behind a base that changed workflow files) was sent again every sweep: projectbluefin `fsdk-containers#508` logged "update-branch refused" about every 75 s. A merge GitHub refused for want of an approving review was sent again every minute instead of every 30 minutes. The trusted-author merge-forward was tried every sweep instead of once per head. "Logged once per head" holds, such as "merge train: PR left out of every train", were logged on every sweep (about 15 PRs every 80 s). The memory now lives on the long-lived GitHub client and is shared by every lane's engine. A read-only integration-eligibility check keeps its own memory, so a check that makes no write never marks a head as tried. diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index 8adc14de7e..9da6787872 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -162,6 +162,10 @@ type contributorLaneFixture struct { labelsCreated int labelsAdded []string labelsRemoved []string + // updateStatus, when set, is the status every PUT update-branch + // answers with, carrying updateMessage. + updateStatus int + updateMessage string } func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.Server { @@ -436,6 +440,12 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S json.NewEncoder(w).Encode(commit) case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/update-branch": fx.updates++ + if fx.updateStatus != 0 { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(fx.updateStatus) + json.NewEncoder(w).Encode(map[string]any{"message": fx.updateMessage}) + return + } json.NewEncoder(w).Encode(map[string]any{"message": "Updating pull request branch."}) case r.Method == http.MethodPost && r.URL.Path == "/repos/acme/widget/labels": fx.labelsCreated++ diff --git a/src/pkg/github/automerge/automerge_held_runs.go b/src/pkg/github/automerge/automerge_held_runs.go index be1f707653..c38a36a75d 100644 --- a/src/pkg/github/automerge/automerge_held_runs.go +++ b/src/pkg/github/automerge/automerge_held_runs.go @@ -677,16 +677,7 @@ func (c *Engine) markRebaseNudged(owner, repo string, number int, headSHA string // and reports whether this is the first report. func (c *Engine) markTerminalHoldLogged(owner, repo string, number int, headSHA, reason string) bool { key := c.evaluatedHeadKey(owner, repo, number) + "@" + strings.ToLower(headSHA) + "@" + reason - c.heldRunApprovalsMu.Lock() - defer c.heldRunApprovalsMu.Unlock() - if c.terminalHoldsLogged == nil { - c.terminalHoldsLogged = make(map[string]bool) - } - if c.terminalHoldsLogged[key] { - return false - } - c.terminalHoldsLogged[key] = true - return true + return c.memory().markHoldLogged(key, c.clock()) } // reportTerminalHold logs a terminal hold once per PR head and reason, and diff --git a/src/pkg/github/automerge/automerge_integration_eligibility.go b/src/pkg/github/automerge/automerge_integration_eligibility.go index 46c7b64359..33f1686d53 100644 --- a/src/pkg/github/automerge/automerge_integration_eligibility.go +++ b/src/pkg/github/automerge/automerge_integration_eligibility.go @@ -44,6 +44,9 @@ func integrationEligibilityProbe(ctx context.Context) bool { func (c *Engine) integrationReadOnlyOptions() Options { options := c.integrationOptions options.MutationBoundary = integrationEligibilityReadOnlyBoundary{} + // Its one-per-head marks (a merge-forward tried, a hold logged) must + // not reach the sweeps: the probe makes no write. + options.isolatedSweepMemory = true options.StageTracker = NewStageTracker() options.BasePreparation = NewBasePreparationStore("") // A private held-run ledger: the contributor lane claims a head before it diff --git a/src/pkg/github/automerge/automerge_merge_forward.go b/src/pkg/github/automerge/automerge_merge_forward.go index db849b7d70..74f49c1137 100644 --- a/src/pkg/github/automerge/automerge_merge_forward.go +++ b/src/pkg/github/automerge/automerge_merge_forward.go @@ -29,8 +29,8 @@ func (c *Engine) mergeForwardTrustedHumanFor(repo string) bool { return c.mergeForwardTrustedHuman(repo) } -// mergeForwardKey namespaces the per-head attempt in heldRunApprovals so it -// never collides with the rebase-nudge or bot-takeover entries. +// mergeForwardKey namespaces the per-head attempt in the sweep memory +// (automerge_sweep_memory.go), which every lane's engine shares. func mergeForwardKey(prKey, headSHA string) string { return prKey + "@" + headSHA + "#merge-forward" } @@ -48,22 +48,15 @@ func mergeForwardWorkflowRefusedKey(prKey, headSHA string) string { func (c *Engine) mergeForwardWorkflowRefused(owner, repo string, number int, headSHA string) bool { key := mergeForwardWorkflowRefusedKey(c.evaluatedHeadKey(owner, repo, number), headSHA) - c.heldRunApprovalsMu.Lock() - defer c.heldRunApprovalsMu.Unlock() - return c.heldRunApprovals[key] + return c.memory().hasOnce(key, c.clock()) } +// markMergeForwardOnce records key in the sweep memory and reports whether +// this is its first use. The memory outlives the one-shot engine the +// trusted-author lane runs on each tick, so a head is tried once per +// process, not once per sweep. func (c *Engine) markMergeForwardOnce(key string) bool { - c.heldRunApprovalsMu.Lock() - defer c.heldRunApprovalsMu.Unlock() - if c.heldRunApprovals == nil { - c.heldRunApprovals = make(map[string]bool) - } - if c.heldRunApprovals[key] { - return false - } - c.heldRunApprovals[key] = true - return true + return c.memory().markOnce(key, c.clock()) } // mergeForwardOrConflict handles a trusted-author PR GitHub reports as diff --git a/src/pkg/github/automerge/automerge_merge_refusal.go b/src/pkg/github/automerge/automerge_merge_refusal.go index 2a2a10a8e2..c3f588e682 100644 --- a/src/pkg/github/automerge/automerge_merge_refusal.go +++ b/src/pkg/github/automerge/automerge_merge_refusal.go @@ -147,12 +147,13 @@ func (c *Engine) requiredChecksExpected(a mergeAttempt, err error) bool { // mergeable, so a "blocked"-only memory was never consulted there and the // merge was re-PUT every pass (hivecommons/hive#15419). func (c *Engine) checksExpectedKnown(a mergeAttempt, now time.Time) bool { - r, ok := c.mergeRefusals[mergeRefusalChecksKey(a)] + m := c.memory() + r, ok := m.mergeRefusals[mergeRefusalChecksKey(a)] if !ok { return false } if now.Sub(r.at) >= mergeRefusalRecheck || !strings.EqualFold(r.head, strings.TrimSpace(a.head)) || comparableMergeableState(a.state) != r.state { - delete(c.mergeRefusals, mergeRefusalChecksKey(a)) + delete(m.mergeRefusals, mergeRefusalChecksKey(a)) return false } return true @@ -187,27 +188,28 @@ func (c *Engine) knownMergeRefusal(a mergeAttempt) string { return autoMergeReasonMergeQueueRequired } now := c.clock() - c.mergeRefusalsMu.Lock() - defer c.mergeRefusalsMu.Unlock() + m := c.memory() + m.mergeRefusalsMu.Lock() + defer m.mergeRefusalsMu.Unlock() if c.checksExpectedKnown(a, now) { return autoMergeReasonChecksExpected } if key := mergeRefusalMethodKey(a); strings.TrimSpace(a.method) != "" { - if r, ok := c.mergeRefusals[key]; ok { + if r, ok := m.mergeRefusals[key]; ok { if now.Sub(r.at) < mergeRefusalRecheck { return autoMergeReasonMergeRefusedMethod } - delete(c.mergeRefusals, key) + delete(m.mergeRefusals, key) } } key := mergeRefusalReviewKey(a) - r, ok := c.mergeRefusals[key] + r, ok := m.mergeRefusals[key] if !ok { return "" } state := comparableMergeableState(a.state) if now.Sub(r.at) >= mergeRefusalRecheck || !strings.EqualFold(r.head, strings.TrimSpace(a.head)) || (state != "" && state != r.state) { - delete(c.mergeRefusals, key) + delete(m.mergeRefusals, key) return "" } return autoMergeReasonMergeRefusedReview @@ -251,20 +253,21 @@ func (c *Engine) refusedMerge(a mergeAttempt, err error) string { // entries once the map is large. func (c *Engine) rememberMergeRefusal(key string, r mergeRefusal) { r.at = c.clock() - c.mergeRefusalsMu.Lock() - defer c.mergeRefusalsMu.Unlock() - if c.mergeRefusals == nil { - c.mergeRefusals = make(map[string]mergeRefusal) + m := c.memory() + m.mergeRefusalsMu.Lock() + defer m.mergeRefusalsMu.Unlock() + if m.mergeRefusals == nil { + m.mergeRefusals = make(map[string]mergeRefusal) } - if len(c.mergeRefusals) >= mergeRefusalPruneAt { + if len(m.mergeRefusals) >= mergeRefusalPruneAt { // Entries for PRs that merged or closed are never read again. - for k, old := range c.mergeRefusals { + for k, old := range m.mergeRefusals { if r.at.Sub(old.at) >= mergeRefusalRecheck { - delete(c.mergeRefusals, k) + delete(m.mergeRefusals, k) } } } - c.mergeRefusals[key] = r + m.mergeRefusals[key] = r } func nativeQueueMethodRefusalKey(claim effects.Claim) string { @@ -281,14 +284,15 @@ func (c *Engine) knownNativeQueueMethodRefusal(claim effects.Claim, head string) } key := nativeQueueMethodRefusalKey(claim) now := c.clock() - c.mergeRefusalsMu.Lock() - defer c.mergeRefusalsMu.Unlock() - r, ok := c.mergeRefusals[key] + m := c.memory() + m.mergeRefusalsMu.Lock() + defer m.mergeRefusalsMu.Unlock() + r, ok := m.mergeRefusals[key] if !ok { return false } if now.Sub(r.at) >= mergeRefusalRecheck || !strings.EqualFold(r.head, strings.TrimSpace(head)) { - delete(c.mergeRefusals, key) + delete(m.mergeRefusals, key) return false } return true @@ -301,17 +305,18 @@ func (c *Engine) noteNativeQueueMethodRefusal(claim effects.Claim, head string) return } r := mergeRefusal{head: strings.TrimSpace(head), at: c.clock()} - c.mergeRefusalsMu.Lock() - defer c.mergeRefusalsMu.Unlock() - if c.mergeRefusals == nil { - c.mergeRefusals = make(map[string]mergeRefusal) + m := c.memory() + m.mergeRefusalsMu.Lock() + defer m.mergeRefusalsMu.Unlock() + if m.mergeRefusals == nil { + m.mergeRefusals = make(map[string]mergeRefusal) } - if len(c.mergeRefusals) >= mergeRefusalPruneAt { - for k, old := range c.mergeRefusals { + if len(m.mergeRefusals) >= mergeRefusalPruneAt { + for k, old := range m.mergeRefusals { if r.at.Sub(old.at) >= mergeRefusalRecheck { - delete(c.mergeRefusals, k) + delete(m.mergeRefusals, k) } } } - c.mergeRefusals[nativeQueueMethodRefusalKey(claim)] = r + m.mergeRefusals[nativeQueueMethodRefusalKey(claim)] = r } diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index a0cf5888f8..55776f738e 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -299,6 +299,10 @@ type Options struct { // again; nil records nothing. HotspotRecorder HotspotRecorder IntegrationFilesObserved func(repo, head string, files []string) + // isolatedSweepMemory gives the engine a sweep memory of its own + // instead of the transport's shared one: a read-only evaluation must + // not mark a head as tried (automerge_sweep_memory.go). + isolatedSweepMemory bool } // Engine owns the automerge sweep policy state. @@ -430,9 +434,6 @@ type Engine struct { workflowApprovalScans map[string]time.Time workflowApprovalUntrusted map[string]bool workflowApprovalWorkflowChecks map[string]okToTestWorkflowCheck - // terminalHoldsLogged remembers PR head+reason pairs already reported as a - // terminal held/never-started hold so later ticks only count them. - terminalHoldsLogged map[string]bool // unauthorized remembers, per PR, the head and step this process marked // `hive:unauthorized` for, so the same head logs once and the mark // clears when the step is done; unauthorizedLabelReady remembers the @@ -445,12 +446,13 @@ type Engine struct { // a REST merge that cannot succeed (hivecommons/hive#14068). mergeQueueBasesMu sync.Mutex mergeQueueBases *MergeQueueBases - // mergeRefusals remembers merges GitHub refused because an approving - // review is required (per PR head) or the merge method is disabled (per - // repository and method), so no sweep re-issues them every pass - // (automerge_merge_refusal.go). - mergeRefusalsMu sync.Mutex - mergeRefusals map[string]mergeRefusal + // sweepMemory is what the lanes remember across sweeps: merge-forward + // and refused update-branch marks per head, the merges GitHub refused + // (automerge_merge_refusal.go) and the holds already logged. New shares + // it through the transport so the one-shot engine each tick builds + // keeps it (automerge_sweep_memory.go). + sweepMemoryOnce sync.Once + sweepMemory *sweepMemory // adoptionOriginalChecks throttles the self lane's lookup of an // adoption PR's original (automerge_adoption_original.go). adoptionOriginalChecksMu sync.Mutex @@ -606,6 +608,9 @@ func New(transport Transport, opts Options) *Engine { e.mutation = provider.MutationBoundary() } } + if !opts.isolatedSweepMemory { + e.sweepMemory = sharedSweepMemory(transport) + } return e } diff --git a/src/pkg/github/automerge/automerge_sweep_memory.go b/src/pkg/github/automerge/automerge_sweep_memory.go new file mode 100644 index 0000000000..1fde33c776 --- /dev/null +++ b/src/pkg/github/automerge/automerge_sweep_memory.go @@ -0,0 +1,125 @@ +package automerge + +import ( + "sync" + "time" +) + +// sweepMemory is what the lanes remember across sweeps about a PR head: +// the merge-forward tried on it, an update-branch or merge-forward GitHub +// refused for lack of the workflows permission, a merge GitHub refused +// (automerge_merge_refusal.go), and the holds already logged. +// +// It is shared by every engine built over the same GitHub client, not kept +// on one engine. The hive runs the queued, trusted-author and contributor +// lanes every minute on a one-shot engine (SweepTrustedAuthorAutoMerges, +// SweepContributorLaneAutoMerges, cmd/hive runAutoMergeSweepIfDue), next to +// the long-lived self-authored loop, so a mark on the engine was empty +// again on the next tick: projectbluefin fsdk-containers#508 sent the same +// refused update-branch to GitHub every ~75 s, and the "logged once per +// head" holds were logged every sweep. +// +// An engine over a transport that does not share state (the sweep's test +// fakes, a nil transport) keeps its own memory, as before. A read-only +// evaluation (IntegrationEligibility) gets its own too, so a probe that +// cannot act never marks a head as tried. +type sweepMemory struct { + // onceMu guards once and holdsLogged: key -> when it was last used. + onceMu sync.Mutex + once map[string]time.Time + holdsLogged map[string]time.Time + // mergeRefusalsMu guards mergeRefusals (automerge_merge_refusal.go). + mergeRefusalsMu sync.Mutex + mergeRefusals map[string]mergeRefusal +} + +// sweepMemoryStateName is the name the memory is kept under on the client +// (hgithub.Client.SharedState). +const sweepMemoryStateName = "automerge.sweep-memory" + +// sweepMemoryIdle is how long a once-per-head mark nothing has used stays: +// past it the PR merged, closed or moved to another head long ago. +const sweepMemoryIdle = 24 * time.Hour + +// sweepMemoryPruneAt is the entry count at which idle marks are dropped on +// the next insert. +const sweepMemoryPruneAt = 1024 + +func newSweepMemory() *sweepMemory { return &sweepMemory{} } + +// sharedSweepMemory returns the memory the transport keeps for every engine +// built over it, or nil when it keeps none. +func sharedSweepMemory(transport Transport) *sweepMemory { + provider, ok := transport.(interface { + SharedState(name string, build func() any) any + }) + if !ok { + return nil + } + m, _ := provider.SharedState(sweepMemoryStateName, func() any { return newSweepMemory() }).(*sweepMemory) + return m +} + +// memory returns the engine's sweep memory, building a private one for an +// engine not made by New. +func (c *Engine) memory() *sweepMemory { + c.sweepMemoryOnce.Do(func() { + if c.sweepMemory == nil { + c.sweepMemory = newSweepMemory() + } + }) + return c.sweepMemory +} + +// markOnce records key and reports whether this is its first use. +func (m *sweepMemory) markOnce(key string, now time.Time) bool { + m.onceMu.Lock() + defer m.onceMu.Unlock() + if _, ok := m.once[key]; ok { + m.once[key] = now + return false + } + m.once = sweepMemoryInsert(m.once, key, now) + return true +} + +// hasOnce reports whether key was recorded. +func (m *sweepMemory) hasOnce(key string, now time.Time) bool { + m.onceMu.Lock() + defer m.onceMu.Unlock() + if _, ok := m.once[key]; !ok { + return false + } + m.once[key] = now + return true +} + +// markHoldLogged records a logged hold and reports whether this is its +// first report. +func (m *sweepMemory) markHoldLogged(key string, now time.Time) bool { + m.onceMu.Lock() + defer m.onceMu.Unlock() + if _, ok := m.holdsLogged[key]; ok { + m.holdsLogged[key] = now + return false + } + m.holdsLogged = sweepMemoryInsert(m.holdsLogged, key, now) + return true +} + +// sweepMemoryInsert adds key to set, first dropping marks idle past +// sweepMemoryIdle once the set is large. The caller holds the lock. +func sweepMemoryInsert(set map[string]time.Time, key string, now time.Time) map[string]time.Time { + if set == nil { + set = make(map[string]time.Time) + } + if len(set) >= sweepMemoryPruneAt { + for k, at := range set { + if now.Sub(at) >= sweepMemoryIdle { + delete(set, k) + } + } + } + set[key] = now + return set +} diff --git a/src/pkg/github/automerge/automerge_sweep_memory_test.go b/src/pkg/github/automerge/automerge_sweep_memory_test.go new file mode 100644 index 0000000000..f9565051c3 --- /dev/null +++ b/src/pkg/github/automerge/automerge_sweep_memory_test.go @@ -0,0 +1,252 @@ +package automerge + +// The lanes' once-per-head memory across sweeps. Production runs the queued, +// trusted-author and contributor lanes every minute through the package +// functions (SweepContributorLaneAutoMerges, SweepTrustedAuthorAutoMerges), +// each of which builds a new Engine with New(transport, opts); these tests +// build one engine per sweep on one shared client the same way. +// projectbluefin, 2026-11-08 01:45-02:11Z: fsdk-containers#508 (a fork head +// behind a base that changed workflow files) logged "update-branch refused" +// and sent the same refused PUT update-branch on every sweep. + +import ( + "bytes" + "context" + "log/slog" + "net/http" + "strings" + "sync" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// sweepTickEngines returns a constructor for one engine per sweep tick, all +// over one client and one set of options, as cmd/hive's +// runAutoMergeSweepIfDue builds them. +func sweepTickEngines(apiURL string, opts Options) func() *Engine { + client := hgithub.NewClient("token", "acme", []string{"widget"}, nil, apiURL) + client.SetAppBotLogin(testHiveAppBotLogin) + if opts.BasePreparation == nil { + opts.BasePreparation = NewBasePreparationStore("") + } + if opts.HeadFirstSeen == nil { + opts.HeadFirstSeen = NewHeadFirstSeenTracker() + } + if opts.MergerAuthorizer == nil { + opts.MergerAuthorizer = func(login string) bool { + return testTrustedMergers[strings.ToLower(strings.TrimSpace(login))] + } + } + return func() *Engine { return New(client, opts) } +} + +// contributorLaneSweepClient is the client and options the contributor +// lane's package-level sweep runs with in these tests. +func contributorLaneSweepClient(apiURL string, policy ContributorLanePolicy, clock *contributorLaneClock) (*hgithub.Client, Options) { + client := hgithub.NewClient("token", "acme", []string{"widget"}, nil, apiURL) + client.SetAppBotLogin(testHiveAppBotLogin) + return client, Options{ + ContributorLanePolicy: func() ContributorLanePolicy { return policy }, + ContributorLaneVerdict: approvedVerdict, + ContributorSoak: soakedTracker(clock), + BasePreparation: NewBasePreparationStore(""), + HeadFirstSeen: NewHeadFirstSeenTracker(), + StageStore: stagelabels.NewStore("", nil), + Now: clock.now, + SentinelLabel: func() string { return "hive-sentinel" }, + } +} + +// A fork head GitHub refuses to update (the base changed workflow files) +// is sent once across sweeps, not once per sweep; the lane keeps reporting +// the refusal. +func TestContributorLaneRefusedUpdateBranchNotRetriedAcrossSweeps(t *testing.T) { + fx := validContributorFixture() + fx.headRepo = "someone/widget" + fx.mergeableState = "behind" + fx.updateStatus = http.StatusForbidden + fx.updateMessage = workflowPermissionRefusal + api := newContributorLaneAPI(t, &fx) + defer api.Close() + clock := &contributorLaneClock{at: time.Date(2026, 11, 8, 2, 0, 0, 0, time.UTC)} + client, opts := contributorLaneSweepClient(api.URL, defaultContributorLanePolicy(), clock) + + for i := 1; i <= 3; i++ { + result, err := SweepContributorLaneAutoMerges(context.Background(), client, opts, AutoMergeSweepOptions{}) + if err != nil { + t.Fatalf("sweep %d err = %v", i, err) + } + if len(result.Merged) != 0 || result.Skipped != 1 { + t.Fatalf("sweep %d result = %+v, want one skip", i, result) + } + clock.advance(80 * time.Second) + } + if fx.updates != 1 { + t.Fatalf("update-branch calls = %d across 3 sweeps, want 1", fx.updates) + } +} + +// A merge GitHub refused for want of an approving review is not re-PUT by +// the next sweep's engine inside the recheck window. +func TestContributorLaneRefusedMergeNotRetriedAcrossSweeps(t *testing.T) { + fx := validContributorFixture() + fx.mergeStatus = http.StatusMethodNotAllowed + fx.mergeMessage = "At least 1 approving review is required by reviewers with write access." + api := newContributorLaneAPI(t, &fx) + defer api.Close() + clock := &contributorLaneClock{at: time.Date(2026, 11, 8, 2, 0, 0, 0, time.UTC)} + client, opts := contributorLaneSweepClient(api.URL, defaultContributorLanePolicy(), clock) + + for i := 1; i <= 3; i++ { + result, err := SweepContributorLaneAutoMerges(context.Background(), client, opts, AutoMergeSweepOptions{}) + if err != nil { + t.Fatalf("sweep %d err = %v", i, err) + } + if len(result.Merged) != 0 { + t.Fatalf("sweep %d merged %+v", i, result.Merged) + } + clock.advance(80 * time.Second) + } + if fx.merges != 1 { + t.Fatalf("merge calls = %d across 3 sweeps, want 1", fx.merges) + } + clock.advance(mergeRefusalRecheck) + if _, err := SweepContributorLaneAutoMerges(context.Background(), client, opts, AutoMergeSweepOptions{}); err != nil { + t.Fatalf("sweep after the recheck window err = %v", err) + } + if fx.merges != 2 { + t.Fatalf("merge calls after the recheck window = %d, want 2", fx.merges) + } +} + +// The trusted-author lane's merge-forward is tried once per head across +// sweeps: a conflict is one POST /merges and one note, and a workflow +// refusal keeps reporting workflow-permission without another POST. +func TestMergeForwardTriedOncePerHeadAcrossSweeps(t *testing.T) { + for _, tc := range []struct { + name string + status int + message string + wantReason string + wantComments int + }{ + {name: "conflict", status: http.StatusConflict, wantReason: "conflicting", wantComments: 1}, + {name: "workflow refusal", status: http.StatusForbidden, message: workflowPermissionRefusal, wantReason: autoMergeReasonWorkflowPermission}, + } { + t.Run(tc.name, func(t *testing.T) { + api := &mergeForwardAPI{mergeStatus: tc.status, mergeMessage: tc.message} + srv := newMergeForwardServer(t, api) + defer srv.Close() + tick := sweepTickEngines(srv.URL, Options{MergeForwardTrustedHuman: func(string) bool { return true }}) + pr := mergeForwardPR(false, "author/human") + for i := 1; i <= 3; i++ { + if _, reason, err := tick().mergeForwardOrConflict(context.Background(), "acme/widget", "acme", "widget", pr, 7); reason != tc.wantReason || err != nil { + t.Fatalf("sweep %d: reason, err = %q, %v; want %q", i, reason, err, tc.wantReason) + } + } + api.mu.Lock() + defer api.mu.Unlock() + if api.merges != 1 || len(api.comments) != tc.wantComments { + t.Fatalf("merges, comments = %d, %d across 3 sweeps; want 1, %d", api.merges, len(api.comments), tc.wantComments) + } + }) + } +} + +// The current-base gate's refused update-branch (the path +// fsdk-containers#508 looped on) is sent once per head and base across +// sweeps, and once more when the base moves. +func TestCurrentBaseGateRefusedUpdateNotRetriedAcrossSweeps(t *testing.T) { + head := "sha-head" + var baseMu sync.Mutex + baseTip := "sha-base-1" + compare := func(w http.ResponseWriter, r *http.Request) { + baseMu.Lock() + defer baseMu.Unlock() + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/compare/main...sha-head": + _, _ = w.Write([]byte(`{"behind_by":2,"ahead_by":1,"base_commit":{"sha":"` + baseTip + `"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/main": + _, _ = w.Write([]byte(`{"name":"main","commit":{"sha":"` + baseTip + `"}}`)) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + } + srv, updates := updateBranchRefusalServer(t, workflowPermissionRefusal, &head, compare) + defer srv.Close() + tick := sweepTickEngines(srv.URL, Options{RequireCurrentBase: func(string) bool { return true }}) + sweep := func(step string, wantUpdates int) { + t.Helper() + _, reason, stop, err := tick().currentBaseGate(context.Background(), "acme/widget", "acme", "widget", 7, "main", head, true, "behind", currentBaseTestPR(true, "feature", "alice", "behind")) + if !stop || reason != autoMergeReasonForkWorkflowUpdate || err != nil { + t.Fatalf("%s: stop, reason, err = %v, %q, %v; want true, %q", step, stop, reason, err, autoMergeReasonForkWorkflowUpdate) + } + if got := updates(); got != wantUpdates { + t.Fatalf("%s: update-branch calls = %d, want %d", step, got, wantUpdates) + } + } + sweep("first sweep", 1) + sweep("second sweep", 1) + sweep("third sweep", 1) + baseMu.Lock() + baseTip = "sha-base-2" + baseMu.Unlock() + sweep("the base moved", 2) + sweep("same base again", 2) +} + +// A hold logged "once per head" is logged once across sweeps: on +// projectbluefin "merge train: PR left out of every train" repeated for +// ~15 PRs on every sweep. +func TestMergeTrainSkipLoggedOnceAcrossSweeps(t *testing.T) { + api := &mergeTrainAPI{files: map[int][]string{}, state: map[int]string{}} + srv := newMergeTrainServer(t, api) + defer srv.Close() + var logs bytes.Buffer + tick := sweepTickEngines(srv.URL, Options{ + Logger: slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelInfo})), + MergeTrain: func() MergeTrainPolicy { return DefaultMergeTrainPolicy }, + }) + pr := mtPR(15405, time.Date(2026, 11, 8, 2, 0, 0, 0, time.UTC), "") + pr.Labels = []*gh.Label{{Name: gh.Ptr(stagelabels.Fixing.Label())}} + for i := 0; i < 3; i++ { + tick().planMergeTrains(context.Background(), stageLaneContributor, "acme/widget", "acme", "widget", []*gh.PullRequest{pr}, func(*gh.PullRequest) string { return "" }) + } + if got := strings.Count(logs.String(), "left out of every train"); got != 1 { + t.Fatalf("skip logged %d times across 3 sweeps, want 1\n%s", got, logs.String()) + } + pr.Head.SHA = gh.Ptr("sha15405-2") + tick().planMergeTrains(context.Background(), stageLaneContributor, "acme/widget", "acme", "widget", []*gh.PullRequest{pr}, func(*gh.PullRequest) string { return "" }) + if got := strings.Count(logs.String(), "left out of every train"); got != 2 { + t.Fatalf("a new head logged %d lines in total, want 2", got) + } +} + +// Engines over one client share the memory; a read-only integration probe +// and an engine over a transport that keeps no state each get their own. +func TestSweepMemorySharedPerClient(t *testing.T) { + tick := sweepTickEngines("http://127.0.0.1:0", Options{}) + first, second := tick(), tick() + if first.memory() != second.memory() { + t.Fatal("two sweep engines over one client keep separate memories") + } + probe := New(first.transport, first.integrationReadOnlyOptions()) + if probe.memory() == first.memory() { + t.Fatal("the read-only integration probe shares the sweeps' memory") + } + if !first.markMergeForwardOnce("k") || second.markMergeForwardOnce("k") || !probe.markMergeForwardOnce("k") { + t.Fatal("once marks do not follow the shared memory") + } + other := sweepTickEngines("http://127.0.0.1:0", Options{})() + if other.memory() == first.memory() { + t.Fatal("engines over different clients share a memory") + } + if (&Engine{}).memory() == nil { + t.Fatal("an engine not made by New has no memory") + } +} diff --git a/src/pkg/github/automerge/automerge_v6_lane.go b/src/pkg/github/automerge/automerge_v6_lane.go index 0559908546..d54ab5dc25 100644 --- a/src/pkg/github/automerge/automerge_v6_lane.go +++ b/src/pkg/github/automerge/automerge_v6_lane.go @@ -209,7 +209,7 @@ func branchUpdateWorkflowRefusalReason(fork bool) string { return autoMergeReasonWorkflowPermission } -// branchUpdateWorkflowRefusedKey namespaces, in heldRunApprovals, GitHub's +// branchUpdateWorkflowRefusedKey namespaces, in the sweep memory, GitHub's // workflow-file refusal of update-branch for one head on one base, as // mergeForwardWorkflowRefusedKey does for the merge-forward. func branchUpdateWorkflowRefusedKey(prKey, headSHA, baseSHA string) string { @@ -221,9 +221,7 @@ func (c *Engine) branchUpdateWorkflowRefused(owner, repo string, number int, hea return false } key := branchUpdateWorkflowRefusedKey(c.evaluatedHeadKey(owner, repo, number), headSHA, baseSHA) - c.heldRunApprovalsMu.Lock() - defer c.heldRunApprovalsMu.Unlock() - return c.heldRunApprovals[key] + return c.memory().hasOnce(key, c.clock()) } func (c *Engine) markBranchUpdateWorkflowRefused(owner, repo string, number int, headSHA, baseSHA string) { diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index e5410f6050..f8dfc030d5 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -204,6 +204,10 @@ type Client struct { selfLoginPending chan struct{} // closed when the in-flight GET /user completes additionalBotLogins []string noBotIdentityWarned sync.Once + // sharedState holds process-wide values other packages keep on the + // Client (SharedState), such as the automerge sweeps' once-per-head marks. + sharedStateMu sync.Mutex + sharedState map[string]any // approvalDesk is the RFC #4000 approval-desk consultation performed per PR // by the self-authored auto-merge sweep. nil (the default) means the sweep // behaves exactly as it did before the desk existed. Set by SetApprovalDesk diff --git a/src/pkg/github/shared_state.go b/src/pkg/github/shared_state.go new file mode 100644 index 0000000000..452f54d71d --- /dev/null +++ b/src/pkg/github/shared_state.go @@ -0,0 +1,33 @@ +package github + +// SharedState returns the value this Client keeps under name, building it +// with build on first use. Every later call with the same name returns that +// same value for the life of the Client, which is the life of the hive +// process. +// +// It exists for packages that build short-lived workers over the Client but +// need memory that outlives them. The automerge sweeps run the queued, +// trusted-author and contributor lanes every minute on a one-shot engine +// (automerge.SweepContributorLaneAutoMerges and friends), so a "once per PR +// head" mark kept on the engine was empty again on the next tick. Those +// marks now live here, owned and typed by the package that uses them. +// +// The value must be safe for concurrent use: the long-lived self-authored +// loop and the per-minute sweeps run on different goroutines. A nil Client +// returns a fresh value that nothing else shares. +func (c *Client) SharedState(name string, build func() any) any { + if c == nil { + return build() + } + c.sharedStateMu.Lock() + defer c.sharedStateMu.Unlock() + if v, ok := c.sharedState[name]; ok { + return v + } + v := build() + if c.sharedState == nil { + c.sharedState = map[string]any{} + } + c.sharedState[name] = v + return v +}