From ef740d1bba1c5c556ca863306a0b722860cbc1b4 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:53:43 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20re-adoption=20after=20a?= =?UTF-8?q?=20closed=20hive=20copy=20is=20not=20replayed=20by=20the=20muta?= =?UTF-8?q?tion=20journal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: clubanderson <407614+clubanderson@users.noreply.github.com> --- .../fixed-readopt-after-closed-copy.md | 1 + ...automerge_adoption_readopt_journal_test.go | 217 ++++++++++++++++++ .../automerge/automerge_contributor_adopt.go | 147 ++++++++---- 3 files changed, 321 insertions(+), 44 deletions(-) create mode 100644 changelog.d/fixed-readopt-after-closed-copy.md create mode 100644 src/pkg/github/automerge/automerge_adoption_readopt_journal_test.go diff --git a/changelog.d/fixed-readopt-after-closed-copy.md b/changelog.d/fixed-readopt-after-closed-copy.md new file mode 100644 index 0000000000..ecde5f7aad --- /dev/null +++ b/changelog.d/fixed-readopt-after-closed-copy.md @@ -0,0 +1 @@ +- When hive closed its own copy of an adopted PR, re-adopting it now opens a fresh copy instead of being mistaken for the earlier adoption and undone, and a recorded copy that is still open is reused ([#15204](https://github.com/hivecommons/hive/issues/15204)). diff --git a/src/pkg/github/automerge/automerge_adoption_readopt_journal_test.go b/src/pkg/github/automerge/automerge_adoption_readopt_journal_test.go new file mode 100644 index 0000000000..ab9a224dbb --- /dev/null +++ b/src/pkg/github/automerge/automerge_adoption_readopt_journal_test.go @@ -0,0 +1,217 @@ +package automerge + +import ( + "context" + "sort" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/effects" +) + +// Re-adoption after a closed copy under a journaling mutation boundary: the +// second adoption must not be answered by the journal entry of the first. + +// journalBoundary mimics the mutation boundary's journal for adoption +// claims: a branch_push or pull_request_create claim whose logical effect +// (repo, kind, target, inputs) already applied is answered with the recorded +// provenance and its effect does not run. +type journalBoundary struct { + mu sync.Mutex + applied map[string]string + claims []effects.Claim + replays []effects.Claim + // seed answers a claim the journal has not seen with recorded + // provenance, as if an earlier process had applied it. + seed func(effects.Claim) (string, bool) +} + +func journalKey(claim effects.Claim) string { + keys := make([]string, 0, len(claim.Inputs)) + for k := range claim.Inputs { + keys = append(keys, k) + } + sort.Strings(keys) + parts := []string{claim.Repo, claim.Kind, claim.Target} + for _, k := range keys { + parts = append(parts, k+"="+claim.Inputs[k]) + } + return strings.Join(parts, "|") +} + +func (b *journalBoundary) Execute(ctx context.Context, claim effects.Claim, effect func(context.Context) (effects.Result, error)) (effects.Result, error) { + if claim.Kind != effects.KindBranchPush && claim.Kind != effects.KindPullRequestCreate { + return effect(ctx) + } + key := journalKey(claim) + b.mu.Lock() + b.claims = append(b.claims, claim) + prov, ok := b.applied[key] + if !ok && b.seed != nil { + prov, ok = b.seed(claim) + } + if ok { + b.replays = append(b.replays, claim) + b.mu.Unlock() + return effects.Result{Provenance: prov}, nil + } + b.mu.Unlock() + res, err := effect(ctx) + if err == nil { + b.mu.Lock() + b.applied[key] = res.Provenance + b.mu.Unlock() + } + return res, err +} + +func (b *journalBoundary) replayed(kind string) []effects.Claim { + b.mu.Lock() + defer b.mu.Unlock() + var out []effects.Claim + for _, c := range b.replays { + if c.Kind == kind { + out = append(out, c) + } + } + return out +} + +func (b *journalBoundary) lastClaim(kind string) effects.Claim { + b.mu.Lock() + defer b.mu.Unlock() + for i := len(b.claims) - 1; i >= 0; i-- { + if b.claims[i].Kind == kind { + return b.claims[i] + } + } + return effects.Claim{} +} + +// adoptAt074Journaled is adoptAt074 with the journal wired in before the +// first adoption, so the claim that opened copy #90 is on record. +func adoptAt074Journaled(t *testing.T) (*adoptHarness, *journalBoundary, func(), []string) { + t.Helper() + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, adoptPolicy()) + jb := &journalBoundary{applied: map[string]string{}} + h.e.mutation = jb + h.observe(adoptPR(7, "aaa"), "check-failing:unit") + labels := []string{"stage/072-returned-to-contributor", "role/contributor", "author/clanker"} + h.clock.advance(time.Hour) + h.observe(adoptPR(7, "aaa", labels...), "check-failing:unit") + h.observe(adoptPR(7, "aaa", labels...), "check-failing:unit") + if len(h.adopt.pulls) != 1 || len(jb.replays) != 0 { + t.Fatalf("setup: pulls=%d replays=%v", len(h.adopt.pulls), jb.replays) + } + return h, jb, done, []string{"stage/074-adopted-by-hive", "role/contributor", "author/clanker"} +} + +// The copy is closed unmerged; the re-adoption claims a distinct effect +// (prior_copy=90), so the journal does not replay the first claim and a +// fresh copy opens instead of the adoption being reverted. +func TestAdoptionReadoptAfterClosedCopyIsNotJournalReplayed(t *testing.T) { + h, jb, done, adopted := adoptAt074Journaled(t) + defer done() + h.adopt.setSuperseder(90, "closed", false) + h.api.reset() + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if r := jb.replayed(effects.KindPullRequestCreate); len(r) != 0 { + t.Fatalf("re-adoption replayed the first adoption's claim: %v", r) + } + if r := jb.replayed(effects.KindBranchPush); len(r) != 0 { + t.Fatalf("re-adoption replayed the first adoption's push: %v", r) + } + for _, kind := range []string{effects.KindBranchPush, effects.KindPullRequestCreate} { + if got := jb.lastClaim(kind).Inputs["prior_copy"]; got != "90" { + t.Fatalf("%s claim prior_copy = %q, want 90", kind, got) + } + } + if len(h.adopt.pulls) != 2 { + t.Fatalf("no fresh copy opened: pulls=%d", len(h.adopt.pulls)) + } + if n := commentsWithPrefix(h.api, AdoptionRevertedMarker); len(n) != 0 { + t.Fatalf("adoption reverted: %v", n) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 91 || rec.AdoptedHead != "aaa" || rec.AdoptOrphanedBy != 90 { + t.Fatalf("record after re-adoption = %+v", rec) + } +} + +// The journal replays the PR-create claim and hands back a copy that is +// closed: the adoption claims a fresh copy rather than reading the replay +// as a failure and clearing the adoption. +func TestAdoptionJournalReplayOfClosedCopyOpensFreshCopy(t *testing.T) { + h, jb, done, adopted := adoptAt074Journaled(t) + defer done() + h.adopt.setSuperseder(55, "closed", false) + jb.seed = func(c effects.Claim) (string, bool) { + if c.Kind == effects.KindPullRequestCreate && c.Inputs["prior_copy"] == "90" { + return "https://github.com/acme/widget/pull/55", true + } + return "", false + } + h.adopt.setSuperseder(90, "closed", false) + h.api.reset() + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if len(h.adopt.pulls) != 2 { + t.Fatalf("closed replay did not lead to a fresh copy: pulls=%d", len(h.adopt.pulls)) + } + if got := jb.lastClaim(effects.KindPullRequestCreate).Inputs["prior_copy"]; got != "55" { + t.Fatalf("fresh claim prior_copy = %q, want 55", got) + } + if n := commentsWithPrefix(h.api, AdoptionRevertedMarker); len(n) != 0 { + t.Fatalf("adoption cleared on a closed replay: %v", n) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 91 || rec.AdoptedHead != "aaa" { + t.Fatalf("record after closed replay = %+v", rec) + } +} + +// The journal replays the PR-create claim and hands back a copy that is +// still open: that copy is the adoption, and no second one is opened. +func TestAdoptionJournalReplayOfOpenCopyAdoptsIt(t *testing.T) { + h, jb, done, adopted := adoptAt074Journaled(t) + defer done() + h.adopt.setSuperseder(55, "open", false) + jb.seed = func(c effects.Claim) (string, bool) { + if c.Kind == effects.KindPullRequestCreate && c.Inputs["prior_copy"] == "90" { + return "https://github.com/acme/widget/pull/55", true + } + return "", false + } + h.adopt.setSuperseder(90, "closed", false) + h.api.reset() + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if len(h.adopt.pulls) != 1 { + t.Fatalf("a copy was opened beside the replayed open one: pulls=%d", len(h.adopt.pulls)) + } + markers := adoptionComments(h.api) + if len(markers) != 1 || !strings.Contains(markers[0], "superseded_by=55") { + t.Fatalf("adoption marker: %v", h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 55 || rec.AdoptedHead != "aaa" { + t.Fatalf("record after open replay = %+v", rec) + } +} + +func TestAdoptionReplayedPRNumber(t *testing.T) { + for in, want := range map[string]int{ + "https://github.com/acme/widget/pull/55": 55, + "https://github.com/acme/widget/pull/55/": 55, + "": 0, + "abc123": 0, + "https://github.com/acme/widget/pull/x": 0, + } { + if got := adoptionReplayedPRNumber(in); got != want { + t.Fatalf("adoptionReplayedPRNumber(%q) = %d, want %d", in, got, want) + } + } +} diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index b7dea5f46c..bf5b33d5e5 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -758,42 +758,62 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor } branch := adoptBranchPrefix + strconv.Itoa(number) fullRepo := owner + "/" + repo + // A re-adoption after the hive's copy was closed unmerged + // (recoverOrphanedAdoption) is a distinct effect from the adoption that + // opened the dead copy: without the dead number in the claim, the + // mutation journal replays the first claim as already applied, no copy + // opens and the adoption is reverted. + priorCopy := 0 + if stored, ok := store.Get(key); ok && stored.AdoptedBy > 0 && stored.AdoptOrphanedBy == stored.AdoptedBy { + priorCopy = stored.AdoptedBy + } + claimInputs := func(prior int, inputs map[string]string) map[string]string { + if prior > 0 { + inputs["prior_copy"] = strconv.Itoa(prior) + } + return inputs + } // resignedTip stays "" when the branch falls back to the unsigned // contributor head; the body then keeps the manual DCO repair steps. var resignedTip string - _, err = effects.Execute(ctx, c.mutation, effects.Claim{ - Repo: fullRepo, Kind: effects.KindBranchPush, Target: branch, Actor: "stage_labels", - Inputs: map[string]string{"pr": strconv.Itoa(number), "head_sha": obs.HeadSHA, "adopt": "true"}, - }, func(ctx context.Context) (effects.Result, error) { - tip := obs.HeadSHA - if dcoRehome { - if signed, signErr := c.resignAdoptionCommits(ctx, obs); signErr != nil { - c.warn("stage labels: re-signing adopted commits failed; branching from the unsigned head", "repo", obs.Display, "pr", number, "error", signErr) - } else if signed != "" { - tip = signed - resignedTip = signed + pushBranch := func(prior int) error { + _, err := effects.Execute(ctx, c.mutation, effects.Claim{ + Repo: fullRepo, Kind: effects.KindBranchPush, Target: branch, Actor: "stage_labels", + Inputs: claimInputs(prior, map[string]string{"pr": strconv.Itoa(number), "head_sha": obs.HeadSHA, "adopt": "true"}), + }, func(ctx context.Context) (effects.Result, error) { + tip := obs.HeadSHA + if dcoRehome { + if signed, signErr := c.resignAdoptionCommits(ctx, obs); signErr != nil { + c.warn("stage labels: re-signing adopted commits failed; branching from the unsigned head", "repo", obs.Display, "pr", number, "error", signErr) + } else if signed != "" { + tip = signed + resignedTip = signed + } } - } - _, _, apiErr := c.gh.Git.CreateRef(ctx, owner, repo, &gh.Reference{ - Ref: gh.Ptr("refs/heads/" + branch), - Object: &gh.GitObject{SHA: gh.Ptr(tip)}, - }) - if apiErr != nil && isGitHubStatus(apiErr, http.StatusUnprocessableEntity) { - // The branch exists from an earlier attempt: point it at this head. - _, _, apiErr = c.gh.Git.UpdateRef(ctx, owner, repo, &gh.Reference{ + _, _, apiErr := c.gh.Git.CreateRef(ctx, owner, repo, &gh.Reference{ Ref: gh.Ptr("refs/heads/" + branch), Object: &gh.GitObject{SHA: gh.Ptr(tip)}, - }, true) - } - if apiErr != nil { - return effects.Result{}, effects.NotApplied(apiErr) + }) + if apiErr != nil && isGitHubStatus(apiErr, http.StatusUnprocessableEntity) { + // The branch exists from an earlier attempt: point it at this head. + _, _, apiErr = c.gh.Git.UpdateRef(ctx, owner, repo, &gh.Reference{ + Ref: gh.Ptr("refs/heads/" + branch), + Object: &gh.GitObject{SHA: gh.Ptr(tip)}, + }, true) + } + if apiErr != nil { + return effects.Result{}, effects.NotApplied(apiErr) + } + return effects.Result{Provenance: tip}, nil + }) + if err != nil { + // Close-first (hivecommons/hive#15135) orders push → copy → close, + // so a failed push leaves the original open; nothing to reopen. + c.warn("stage labels: adoption failed to push branch", "repo", obs.Display, "pr", number, "branch", branch, "error", err) } - return effects.Result{Provenance: tip}, nil - }) - if err != nil { - // Close-first (hivecommons/hive#15135) orders push → copy → close, - // so a failed push leaves the original open; nothing to reopen. - c.warn("stage labels: adoption failed to push branch", "repo", obs.Display, "pr", number, "branch", branch, "error", err) + return err + } + if err := pushBranch(priorCopy); err != nil { return 0, err } if supersedeHeadConflicts(obs) { @@ -806,23 +826,48 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor } } - var created *gh.PullRequest - _, err = effects.Execute(ctx, c.mutation, effects.Claim{ - Repo: fullRepo, Kind: effects.KindPullRequestCreate, Target: branch, Actor: "stage_labels", - Inputs: map[string]string{"base": obs.BaseRef, "supersedes": strconv.Itoa(number), "head_sha": obs.HeadSHA}, - }, func(ctx context.Context) (effects.Result, error) { - var apiErr error - created, _, apiErr = c.gh.PullRequests.Create(ctx, owner, repo, &gh.NewPullRequest{ - Title: gh.Ptr(nonEmpty(strings.TrimSpace(obs.Title), fmt.Sprintf("Adopt #%d", number))), - Head: gh.Ptr(branch), - Base: gh.Ptr(obs.BaseRef), - Body: gh.Ptr(AdoptionPRBody(obs, rec, dcoRehome, resignedTip)), + // openCopy returns the PR it opened, or nil and the journal's recorded + // provenance when the claim was replayed and the effect did not run. + openCopy := func(prior int) (*gh.PullRequest, string, error) { + var created *gh.PullRequest + res, err := effects.Execute(ctx, c.mutation, effects.Claim{ + Repo: fullRepo, Kind: effects.KindPullRequestCreate, Target: branch, Actor: "stage_labels", + Inputs: claimInputs(prior, map[string]string{"base": obs.BaseRef, "supersedes": strconv.Itoa(number), "head_sha": obs.HeadSHA}), + }, func(ctx context.Context) (effects.Result, error) { + var apiErr error + created, _, apiErr = c.gh.PullRequests.Create(ctx, owner, repo, &gh.NewPullRequest{ + Title: gh.Ptr(nonEmpty(strings.TrimSpace(obs.Title), fmt.Sprintf("Adopt #%d", number))), + Head: gh.Ptr(branch), + Base: gh.Ptr(obs.BaseRef), + Body: gh.Ptr(AdoptionPRBody(obs, rec, dcoRehome, resignedTip)), + }) + if apiErr != nil { + return effects.Result{}, effects.NotApplied(apiErr) + } + return effects.Result{Provenance: created.GetHTMLURL()}, nil }) - if apiErr != nil { - return effects.Result{}, effects.NotApplied(apiErr) + return created, res.Provenance, err + } + created, provenance, err := openCopy(priorCopy) + if replayed := adoptionReplayedPRNumber(provenance); err == nil && created == nil && replayed > 0 && replayed != priorCopy { + // The journal replayed an earlier claim and named the PR it opened: + // an open one is this adoption's copy; a closed-unmerged one is dead, + // so the copy is claimed afresh rather than read as a failure. + pr, _, getErr := c.gh.PullRequests.Get(ctx, owner, repo, replayed) + switch { + case getErr != nil: + err = getErr + case strings.EqualFold(pr.GetState(), "open"): + c.info("stage labels: adoption claim replayed an open App PR; adopting it", "repo", obs.Display, "pr", number, "branch", branch, "replayed", replayed) + created = pr + case !pr.GetMerged(): + c.info("stage labels: adoption claim replayed a closed App PR; opening a fresh copy", "repo", obs.Display, "pr", number, "branch", branch, "replayed", replayed) + if err = pushBranch(replayed); err != nil { + return 0, err + } + created, _, err = openCopy(replayed) } - return effects.Result{Provenance: created.GetHTMLURL()}, nil - }) + } if err != nil || created == nil { c.warn("stage labels: adoption failed to open App PR", "repo", obs.Display, "pr", number, "branch", branch, "error", err) return 0, err @@ -929,6 +974,20 @@ func (c *Engine) replaySupersedeOntoBase(ctx context.Context, store *stagelabels return true, 1, nil } +// adoptionReplayedPRNumber reads the PR number from a pull_request_create +// provenance URL (…/pull/N); 0 when there is none. +func adoptionReplayedPRNumber(provenance string) int { + _, tail, ok := strings.Cut(strings.TrimSpace(provenance), "/pull/") + if !ok { + return 0 + } + n, err := strconv.Atoi(strings.TrimRight(tail, "/")) + if err != nil || n <= 0 { + return 0 + } + return n +} + // resignAdoptionCommits replays the PR's commits with the App's // Signed-off-by added where missing (the API equivalent of // `git rebase --signoff`), keeping each commit's tree, author and message,