From ff3166c644a3895916070cdc541c73884df31bf4 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Tue, 27 Oct 2026 21:23:52 -0400 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B=20dco:=20re-home=20unsigned=20?= =?UTF-8?q?commits=20on=20adopted=20PRs=20so=20the=20gate=20can=20clear?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The in-place DCO repair mode was decided by adoptionDCORepairMode but never executed, so an adopted PR whose unsigned commits were the hive's own sat at stage 074 with reason=dco-missing forever. The supersede replay also refused any head containing a merge commit, which the update-branch recovery adds. - In-place repair is now implemented for base-repo heads: replay the PR commits through the git-data API with Signed-off-by appended, preserving author/message/tree/parents, and force-update the ref only if it still points at the observed head (once per head). - Fork heads always convert to supersede (dco-rehome): the installation token cannot rewrite a fork branch. - resignAdoptionCommits maps parents inside the PR to their rewritten SHAs so merge commits are replayed instead of aborting the re-sign. Fixes #14666 Signed-off-by: Andy Anderson --- changelog.d/fixed-dco-rehome-adopted.md | 1 + src/docs/labels-and-control-signals.md | 28 ++- .../automerge/automerge_contributor_adopt.go | 124 ++++++++++-- .../automerge_contributor_adopt_test.go | 188 ++++++++++++++++-- .../automerge/automerge_stage_labels.go | 1 + .../automerge/automerge_stage_labels_test.go | 12 +- src/pkg/stageengine/prstage/types.go | 3 + src/pkg/stagelabels/store.go | 25 +++ 8 files changed, 346 insertions(+), 36 deletions(-) create mode 100644 changelog.d/fixed-dco-rehome-adopted.md diff --git a/changelog.d/fixed-dco-rehome-adopted.md b/changelog.d/fixed-dco-rehome-adopted.md new file mode 100644 index 0000000000..865bd64c69 --- /dev/null +++ b/changelog.d/fixed-dco-rehome-adopted.md @@ -0,0 +1 @@ +- Adopted contributor PRs whose unsigned commits are the hive's own no longer hold at stage 074 with `dco-missing`: the stage engine re-signs them itself (in place on base-repo heads, re-homed onto `hive/adopt-` on forks) and the replay now carries merge commits from a base sync instead of refusing to re-sign (#14666) diff --git a/src/docs/labels-and-control-signals.md b/src/docs/labels-and-control-signals.md index 75c66a2e22..417b781af7 100644 --- a/src/docs/labels-and-control-signals.md +++ b/src/docs/labels-and-control-signals.md @@ -1082,17 +1082,23 @@ When a clanker-authored contributor PR goes red the hive does not just relabel i contributor-controlled and never count. The stage pass caches that split with the commit check (`dco_unsigned_hive`), so the decision does not depend on which labels the PR currently carries. When every unsigned - commit is App-linked and the branch is still pushable (base-repo head, or - `maintainer_can_modify` re-checked with one `GET pulls/N`), an in-place - adoption stays in place and the fixer rewrites just that unsigned range - with `-s` and force-pushes with lease. If any unsigned commit is - contributor-authored or unknown, or the branch cannot be pushed, the - adoption converts to `mode=supersede` (`reason=dco-rehome`): the hive - pushes `hive/adopt-` at the head, opens the superseding PR and the fix - agent re-homes the diff onto that hive-owned branch from the base: - cherry-pick each original commit with `git cherry-pick -s`, or squash with - `git commit --author= -s`, preserving authorship/credits - while adding the hive submitter sign-off. The replacement PR keeps the + commit is App-linked and the head lives in the **base repo**, an in-place + adoption stays in place and the stage engine itself re-signs the branch + once per head (`reason=dco-rehomed`): it replays the PR's commits through + the git-data API, keeping each author, message, tree and parents (merge + commits from a base sync included) and appending the App's + `Signed-off-by`, then force-updates the ref only if it still points at the + observed head. The engine's installation token cannot rewrite a fork + branch, so a fork head — even one whose only unsigned commits are the + hive's own — and any head with a contributor-authored or unknown unsigned + commit converts to `mode=supersede` (`reason=dco-rehome`): the hive + pushes `hive/adopt-` at the head with the same re-signed replay + (falling back to the unsigned head only when the replay cannot be + built), opens the superseding PR and the fix agent re-homes the diff onto + that hive-owned branch from the base: cherry-pick each original commit + with `git cherry-pick -s`, or squash with `git commit --author= -s`, preserving authorship/credits while adding the hive + submitter sign-off ([#14666](https://github.com/hivecommons/hive/issues/14666)). The replacement PR keeps the original title, starts with `Supersedes #N`, carries the original `Fixes/Closes/Resolves` target (including `Hive-Run: owner/repo#NN` as `Fixes #NN`), and the original PR is commented and closed so it does not diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index d08635f814..82c7f313f6 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -282,16 +282,35 @@ func adoptionNeedsDCORehome(obs stageObservation, canPushInPlace bool) bool { return adoptionDCORepairMode(obs, canPushInPlace) == stagelabels.AdoptModeSupersede } +// adoptionDCORepairMode decides how a DCO-red head gets its sign-off: +// AdoptModeInPlace rewrites the hive's own unsigned commits on the PR +// branch, which the stage pass can only do through the git-data API of the +// base repository — a fork branch is pushable by an agent's git but not by +// the App's REST token, so a fork head re-homes (AdoptModeSupersede) even +// when every unsigned commit is the hive's (hivecommons/hive#14666). func adoptionDCORepairMode(obs stageObservation, canPushInPlace bool) string { if obs.Detail.DCO != stagelabels.DCONo || obs.Detail.DCOUnsigned <= 0 { return "" } - if canPushInPlace && obs.Detail.DCOUnsignedHive == obs.Detail.DCOUnsigned && obs.Detail.DCOUnsignedOther == 0 { + if canPushInPlace && headInBaseRepo(obs) && obs.Detail.DCOUnsignedHive == obs.Detail.DCOUnsigned && obs.Detail.DCOUnsignedOther == 0 { return stagelabels.AdoptModeInPlace } return stagelabels.AdoptModeSupersede } +// headInBaseRepo reports whether the PR head lives in the base repository +// (not a fork), where the App's git-data API can rewrite it. +func headInBaseRepo(obs stageObservation) bool { + return obs.HeadRepo != "" && strings.EqualFold(obs.HeadRepo, obs.Owner+"/"+obs.Repo) +} + +// adoptedHeadNeedsDCORepair decides, for an in-place-adopted PR, whether the +// unsigned commits are the hive's own on a base-repo branch it rewrites in +// place with its sign-off. +func adoptedHeadNeedsDCORepair(obs stageObservation, rec stagelabels.Record, canPushInPlace bool) bool { + return adoptedHeadDCORed(obs, rec) && adoptionDCORepairMode(obs, canPushInPlace) == stagelabels.AdoptModeInPlace +} + // adoptedHeadDCORed reports whether an in-place-adopted PR's head carries // unsigned commits at all (the cheap pre-check before resolving push access). func adoptedHeadDCORed(obs stageObservation, rec stagelabels.Record) bool { @@ -759,12 +778,15 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor return 1, nil } -// resignAdoptionCommits replays the contributor's commits as a linear chain -// with the App's Signed-off-by added where missing (the API equivalent of +// 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, -// and returns the new tip. It returns "" when the commits cannot be replayed -// without changing content (a merge commit, or no commits), so the caller -// keeps the contributor head. +// and returns the new tip. Parents inside the PR map to their rewritten +// counterparts, so a base-sync merge commit (which the DCO checks exempt and +// hivecommons/hive#14500's recovery adds) replays with its tree and both +// parents rather than refusing the rewrite (hivecommons/hive#14666). It +// returns "" when the listing does not end at the observed head or a parent +// cannot be resolved, so the caller keeps the unsigned head. func (c *Engine) resignAdoptionCommits(ctx context.Context, obs stageObservation) (string, error) { opts := &gh.ListOptions{PerPage: 100} var all []*gh.RepositoryCommit @@ -782,30 +804,105 @@ func (c *Engine) resignAdoptionCommits(ctx context.Context, obs stageObservation if len(all) == 0 || all[len(all)-1].GetSHA() != obs.HeadSHA { return "", nil } + inPR := make(map[string]bool, len(all)) for _, rc := range all { - if len(rc.Parents) != 1 { + if rc == nil || len(rc.Parents) == 0 || rc.GetSHA() == "" { return "", nil } + inPR[rc.GetSHA()] = true } trailer := hgithub.BotSignedOffByTrailer(c.transport.AppBotLogin()) - parent := all[0].Parents[0].GetSHA() + rewritten := make(map[string]string, len(all)) for _, rc := range all { + parents := make([]*gh.Commit, 0, len(rc.Parents)) + for _, p := range rc.Parents { + sha := p.GetSHA() + if inPR[sha] { + // The listing is oldest-first; a parent inside the PR + // that is not rewritten yet means the order is not + // topological and the replay would detach it. + mapped, ok := rewritten[sha] + if !ok { + return "", nil + } + sha = mapped + } + parents = append(parents, &gh.Commit{SHA: gh.Ptr(sha)}) + } msg := rc.GetCommit().GetMessage() - if !signedOffByRE.MatchString(msg) { + if len(rc.Parents) == 1 && !signedOffByRE.MatchString(msg) { msg = strings.TrimRight(msg, "\n") + "\n\n" + trailer + "\n" } created, _, err := c.gh.Git.CreateCommit(hgithub.WithRESTCaller(ctx, "hive:automerge_sweep"), obs.Owner, obs.Repo, &gh.Commit{ Message: gh.Ptr(msg), Tree: &gh.Tree{SHA: gh.Ptr(rc.GetCommit().GetTree().GetSHA())}, - Parents: []*gh.Commit{{SHA: gh.Ptr(parent)}}, + Parents: parents, Author: rc.GetCommit().GetAuthor(), }, nil) if err != nil { return "", fmt.Errorf("re-signing %s: %w", shortSHA(rc.GetSHA()), err) } - parent = created.GetSHA() + rewritten[rc.GetSHA()] = created.GetSHA() + } + return rewritten[obs.HeadSHA], nil +} + +// adoptionDCORepaired is the reason logged and recorded when the hive +// rewrote its own unsigned commits in place with its sign-off. +const adoptionDCORepaired = "dco-rehomed" + +// repairAdoptedDCOInPlace rewrites the unsigned hive commits of an +// in-place-adopted base-repo head with the App's Signed-off-by and +// force-updates the PR branch to the new tip — once per head, with the ref +// re-read first so a push that raced the rewrite is not overwritten +// (hivecommons/hive#14666). The next sweep re-reads the new head's commits +// and the DCO gate clears. Returns 1 when the branch moved. +func (c *Engine) repairAdoptedDCOInPlace(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, rec stagelabels.Record) (int, error) { + if rec.DCORepairAttempted(obs.HeadSHA) { + return 0, nil + } + if obs.HeadRef == "" { + c.warn("stage labels: in-place DCO repair skipped, head ref unknown", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA) + return 0, nil + } + store.MarkDCORepair(key, obs.HeadSHA) + c.info("stage labels: re-signing hive commits on adopted PR in place", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "branch", obs.HeadRef, "unsigned_hive", obs.Detail.DCOUnsignedHive, "reason", adoptionDCORepaired) + ref := "refs/heads/" + obs.HeadRef + var tip string + _, err := effects.Execute(ctx, c.mutation, effects.Claim{ + Repo: obs.Owner + "/" + obs.Repo, Kind: effects.KindBranchPush, Target: obs.HeadRef, Actor: "stage_labels", + Inputs: map[string]string{"pr": strconv.Itoa(obs.Number), "head_sha": obs.HeadSHA, "dco_repair": "true"}, + }, func(ctx context.Context) (effects.Result, error) { + current, _, apiErr := c.gh.Git.GetRef(ctx, obs.Owner, obs.Repo, ref) + if apiErr != nil { + return effects.Result{}, effects.NotApplied(apiErr) + } + if got := current.GetObject().GetSHA(); got != obs.HeadSHA { + return effects.Result{}, effects.NotApplied(fmt.Errorf("head moved to %s before the rewrite; re-evaluating", shortSHA(got))) + } + signed, signErr := c.resignAdoptionCommits(ctx, obs) + if signErr != nil { + return effects.Result{}, effects.NotApplied(signErr) + } + if signed == "" { + return effects.Result{}, effects.NotApplied(fmt.Errorf("commits of %s cannot be replayed with sign-off", shortSHA(obs.HeadSHA))) + } + if _, _, apiErr := c.gh.Git.UpdateRef(ctx, obs.Owner, obs.Repo, &gh.Reference{ + Ref: gh.Ptr(ref), + Object: &gh.GitObject{SHA: gh.Ptr(signed)}, + }, true); apiErr != nil { + return effects.Result{}, effects.NotApplied(apiErr) + } + tip = signed + return effects.Result{Provenance: signed}, nil + }) + if err != nil { + c.warn("stage labels: in-place DCO repair failed; branch keeps the unsigned head", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "branch", obs.HeadRef, "error", err) + return 0, err } - return parent, nil + store.MarkHivePushedHead(key, tip) + c.info("stage labels: adopted PR re-signed in place", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "new_head", tip, "branch", obs.HeadRef, "reason", adoptionDCORepaired) + return 1, nil } // settleAdoption polls the superseding App PR for a PR in stage 074 (at @@ -829,6 +926,9 @@ func (c *Engine) settleAdoption(ctx context.Context, store *stagelabels.Store, k } if adoptedHeadDCORed(obs, rec) { canPushInPlace := c.canPushToPRBranch(ctx, obs) + if adoptedHeadNeedsDCORepair(obs, rec, canPushInPlace) { + return c.repairAdoptedDCOInPlace(ctx, store, key, obs, rec) + } if adoptedHeadNeedsDCORehome(obs, rec, canPushInPlace) { fence, err := c.adoptionDiffRefusal(ctx, obs) if fence != "" && (err != nil || adoptionFenceDefers(fence)) { diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index 967bcd0244..e449c7609a 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -147,6 +147,16 @@ func (a *adoptAPI) handle(w http.ResponseWriter, r *http.Request) bool { w.WriteHeader(http.StatusCreated) _ = json.NewEncoder(w).Encode(map[string]any{"ref": body.Ref, "object": map[string]string{"sha": body.SHA}}) return true + case r.Method == http.MethodGet && strings.HasPrefix(path, "/repos/acme/widget/git/ref/"): + ref := "refs/" + strings.TrimPrefix(path, "/repos/acme/widget/git/ref/") + sha, ok := a.refs[ref] + if !ok { + w.WriteHeader(http.StatusNotFound) + _ = json.NewEncoder(w).Encode(map[string]string{"message": "Not Found"}) + return true + } + _ = json.NewEncoder(w).Encode(map[string]any{"ref": ref, "object": map[string]string{"sha": sha}}) + return true case r.Method == http.MethodPatch && strings.HasPrefix(path, "/repos/acme/widget/git/refs/"): var body struct { SHA string `json:"sha"` @@ -1151,32 +1161,186 @@ func TestAlreadyAdoptedInPlaceRehomeIsTriedOncePerHead(t *testing.T) { } } -func TestAlreadyAdoptedInPlaceAppAuthoredDCOFailureStaysInPlace(t *testing.T) { +// hiveUnsignedFixture is a signed contributor commit followed by an unsigned +// commit the hive's fix lane pushed (hivecommons/hive#14666: the inherited +// unsigned commit on an adopted PR is the hive's own more often than not). +func hiveUnsignedFixture() []stageCommitFixture { + return []stageCommitFixture{ + {message: "feat: signed contributor\n\nSigned-off-by: Mallory ", authorLogin: "mallory", authorName: "Mallory", authorEmail: "mallory@example.com"}, + {message: "fix: unsigned hive fixer", authorLogin: testHiveAppBotLogin, authorName: "architect", authorEmail: "12345+hive[bot]@users.noreply.github.com"}, + } +} + +// The App's REST token cannot rewrite a fork branch, so an in-place adoption +// whose only unsigned commits are the hive's still re-homes, with the +// commits re-signed, rather than holding at 074 for a rewrite nobody runs. +func TestAlreadyAdoptedInPlaceAppAuthoredDCOFailureOnForkRehomesResigned(t *testing.T) { p := adoptPolicy() p.AdoptInPlace = true p.DCORequired = func(string) (bool, bool) { return true, true } h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) defer done() h.adopt.allowMaintainerEdits(7, true) - h.api.commitDetails = map[int][]stageCommitFixture{7: { - {message: "feat: signed contributor\n\nSigned-off-by: Mallory ", authorLogin: "mallory", authorName: "Mallory", authorEmail: "mallory@example.com"}, - {message: "fix: unsigned hive fixer", authorLogin: testHiveAppBotLogin, authorName: "architect", authorEmail: "12345+hive[bot]@users.noreply.github.com"}, - }} - h.observe(adoptPR(7, "aaa"), "check-failing:DCO") + h.api.commitDetails = map[int][]stageCommitFixture{7: hiveUnsignedFixture()} + h.observe(adoptPR(7, "c1"), "check-failing:DCO") key := stageKey("acme", "widget", 7) if rec, _ := h.e.stageStoreOrShared().Get(key); rec.DCOUnsigned != 1 || rec.DCOUnsignedHive != 1 { t.Fatalf("cached split: %+v", rec) } - h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "aaa", AdoptReasonOffline, h.clock.at) + h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "c1", AdoptReasonOffline, h.clock.at) h.api.reset() h.clock.advance(time.Minute) - res := h.observe(adoptPR(7, "aaa", "stage/074-adopted-by-hive", "role/contributor", "author/clanker", stagelabels.NoDCOLabel), "check-failing:DCO") - if res.Comments != 0 || len(h.adopt.pulls) != 0 || len(h.adopt.refs) != 0 || len(h.adopt.prEdits) != 0 { - t.Fatalf("App-authored unsigned commits should stay in-place for rewrite: res=%+v pulls=%v refs=%v edits=%v comments=%v", res, h.adopt.pulls, h.adopt.refs, h.adopt.prEdits, h.api.comments) + h.observe(adoptPR(7, "c1", "stage/074-adopted-by-hive", "role/contributor", "author/clanker", stagelabels.NoDCOLabel), "check-failing:DCO") + if len(h.adopt.newCommits) != 2 { + t.Fatalf("re-signed commits=%v", h.adopt.newCommits) + } + msg, _ := h.adopt.newCommits[1]["message"].(string) + if !strings.Contains(msg, "fix: unsigned hive fixer") || !signedOffByRE.MatchString(msg) { + t.Fatalf("hive commit not re-signed: %q", msg) + } + if got := h.adopt.refs["refs/heads/hive/adopt-7"]; got != "signed2" { + t.Fatalf("rehome branch sha=%q, want the re-signed tip; refs=%v", got, h.adopt.refs) + } + if len(h.adopt.pulls) != 1 || len(h.adopt.prEdits) != 1 { + t.Fatalf("fork head should re-home: pulls=%v edits=%v", h.adopt.pulls, h.adopt.prEdits) + } + if rec, _ := h.e.stageStoreOrShared().Get(key); rec.AdoptedInPlace() || rec.AdoptReason != AdoptReasonDCORehome { + t.Fatalf("record should be a dco-rehome supersession: %+v", rec) + } +} + +// basePR is adoptPR with its head in the base repository on branch fix/widget. +func basePR(number int, head string, labels ...string) *gh.PullRequest { + pr := adoptPR(number, head, labels...) + pr.Head.Repo = &gh.Repository{FullName: gh.Ptr("acme/widget")} + pr.Head.Ref = gh.Ptr("fix/widget") + return pr +} + +// A base-repo head whose unsigned commits are all the hive's is rewritten in +// place: the commits are replayed with the App's sign-off, the PR branch is +// force-updated to the new tip, no superseding PR opens, the adoption stays +// in place and the push is recorded as the hive's. The rewrite runs once per +// head. +func TestAlreadyAdoptedInPlaceAppAuthoredDCOFailureInBaseRepoResignsInPlace(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + p.DCORequired = func(string) (bool, bool) { return true, true } + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + h.api.commitDetails = map[int][]stageCommitFixture{7: hiveUnsignedFixture()} + h.adopt.refs["refs/heads/fix/widget"] = "c1" + h.observe(basePR(7, "c1"), "check-failing:DCO") + key := stageKey("acme", "widget", 7) + store := h.e.stageStoreOrShared() + store.MarkAdoptedInPlace(key, "c1", AdoptReasonOffline, h.clock.at) + + h.api.reset() + h.clock.advance(time.Minute) + labels := []string{"stage/074-adopted-by-hive", "role/contributor", "author/clanker", stagelabels.NoDCOLabel} + res := h.observe(basePR(7, "c1", labels...), "check-failing:DCO") + if len(h.adopt.newCommits) != 2 { + t.Fatalf("re-signed commits=%v", h.adopt.newCommits) + } + first, _ := h.adopt.newCommits[0]["message"].(string) + second, _ := h.adopt.newCommits[1]["message"].(string) + if strings.Count(first, "Signed-off-by:") != 1 || !strings.Contains(second, "fix: unsigned hive fixer") || !signedOffByRE.MatchString(second) { + t.Fatalf("replay must keep the signed commit and sign the hive's: %q / %q", first, second) + } + if got := h.adopt.refs["refs/heads/fix/widget"]; got != "signed2" || len(h.adopt.refUpdates) != 1 { + t.Fatalf("PR branch sha=%q updates=%v, want force-update to the re-signed tip", got, h.adopt.refUpdates) } - if rec, _ := h.e.stageStoreOrShared().Get(key); !rec.AdoptedInPlace() || rec.AdoptedBy != 0 { - t.Fatalf("record should remain in-place adoption: %+v", rec) + if res.Comments != 0 || len(h.adopt.pulls) != 0 || len(h.adopt.prEdits) != 0 { + t.Fatalf("in-place repair must not supersede or comment: res=%+v pulls=%v edits=%v comments=%v", res, h.adopt.pulls, h.adopt.prEdits, h.api.comments) + } + rec, _ := store.Get(key) + if !rec.AdoptedInPlace() || rec.AdoptedBy != 0 || !rec.HivePushedHead("signed2") || !rec.DCORepairAttempted("c1") { + t.Fatalf("record should stay in place, own the new head and remember the attempt: %+v", rec) + } + + // The same head is not rewritten twice while the lane re-reads it. + h.api.reset() + h.clock.advance(time.Minute) + h.observe(basePR(7, "c1", labels...), "check-failing:DCO") + if len(h.adopt.newCommits) != 2 || len(h.adopt.refUpdates) != 1 { + t.Fatalf("repair retried on the same head: commits=%d updates=%v", len(h.adopt.newCommits), h.adopt.refUpdates) + } +} + +// The branch is re-read before the force-update: a head that moved since +// the lane observed it is left alone, the attempt is recorded, and the error +// surfaces so the sweep re-evaluates the new head. +func TestInPlaceDCORepairRefusesWhenHeadMoved(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + p.DCORequired = func(string) (bool, bool) { return true, true } + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + h.api.commitDetails = map[int][]stageCommitFixture{7: hiveUnsignedFixture()} + h.adopt.refs["refs/heads/fix/widget"] = "bbb" + h.observe(basePR(7, "c1"), "check-failing:DCO") + key := stageKey("acme", "widget", 7) + h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "c1", AdoptReasonOffline, h.clock.at) + + h.api.reset() + h.clock.advance(time.Minute) + labels := []string{"stage/074-adopted-by-hive", "role/contributor", "author/clanker", stagelabels.NoDCOLabel} + h.e.recordStage(stageLaneContributor, "acme/widget", "acme", "widget", basePR(7, "c1", labels...), "check-failing:DCO", false, nil) + h.e.noteStageRole("acme", "widget", 7, stagelabels.RoleContributor) + if _, err := h.e.ApplyStageLabels(context.Background()); err == nil { + t.Fatal("ApplyStageLabels should surface the refused rewrite") + } + if len(h.adopt.newCommits) != 0 || len(h.adopt.refUpdates) != 0 || h.adopt.refs["refs/heads/fix/widget"] != "bbb" { + t.Fatalf("moved head must not be rewritten: commits=%v updates=%v refs=%v", h.adopt.newCommits, h.adopt.refUpdates, h.adopt.refs) + } + if rec, _ := h.e.stageStoreOrShared().Get(key); !rec.AdoptedInPlace() || !rec.DCORepairAttempted("c1") { + t.Fatalf("attempt not recorded or adoption lost: %+v", rec) + } +} + +// A base-sync merge commit on the adopted head (hivecommons/hive#14500's +// recovery) no longer blocks the re-sign: it replays with its tree and both +// parents, the in-PR parent mapped to its rewritten counterpart, and keeps +// its message (the DCO checks exempt merges). +func TestDCORehomeReplaysMergeCommitWithMappedParents(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + p.DCORequired = func(string) (bool, bool) { return true, true } + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + h.adopt.allowMaintainerEdits(7, true) + fixture := hiveUnsignedFixture() + fixture = append(fixture, stageCommitFixture{message: "Merge branch 'v5' into fix/widget", authorLogin: testHiveAppBotLogin, authorName: "hive[bot]", authorEmail: "12345+hive[bot]@users.noreply.github.com", parents: []string{"c1", "base"}}) + h.api.commitDetails = map[int][]stageCommitFixture{7: fixture} + h.observe(adoptPR(7, "c2"), "check-failing:DCO") + key := stageKey("acme", "widget", 7) + h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "c2", AdoptReasonOffline, h.clock.at) + + h.api.reset() + h.clock.advance(time.Minute) + h.observe(adoptPR(7, "c2", "stage/074-adopted-by-hive", "role/contributor", "author/clanker", stagelabels.NoDCOLabel), "check-failing:DCO") + if len(h.adopt.newCommits) != 3 { + t.Fatalf("replayed commits=%v", h.adopt.newCommits) + } + merge := h.adopt.newCommits[2] + msg, _ := merge["message"].(string) + if signedOffByRE.MatchString(msg) { + t.Fatalf("merge commit must keep its message unsigned: %q", msg) + } + parents, _ := merge["parents"].([]any) + if len(parents) != 2 || parents[0] != "signed2" || parents[1] != "base" { + t.Fatalf("merge parents=%v, want [signed2 base]", parents) + } + if got := h.adopt.refs["refs/heads/hive/adopt-7"]; got != "signed3" { + t.Fatalf("rehome branch sha=%q, want the replayed merge tip; refs=%v", got, h.adopt.refs) + } + if len(h.adopt.pulls) != 1 { + t.Fatalf("pulls=%v", h.adopt.pulls) + } + body, _ := h.adopt.pulls[0]["body"].(string) + if !strings.Contains(body, "re-signed as `signed3`") || strings.Contains(body, "**DCO repair required:**") { + t.Fatalf("body must name the re-signed tip and omit the manual DCO block:\n%s", body) } } diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 499ba84b66..4265d33cfe 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -404,6 +404,7 @@ func (c *Engine) recordStage(lane, displayRepo, owner, repo string, pr *gh.PullR } if pr.GetHead() != nil { obs.HeadSHA = pr.GetHead().GetSHA() + obs.HeadRef = pr.GetHead().GetRef() } if pr.GetHead() != nil && pr.GetHead().GetRepo() != nil { obs.HeadRepo = pr.GetHead().GetRepo().GetFullName() diff --git a/src/pkg/github/automerge/automerge_stage_labels_test.go b/src/pkg/github/automerge/automerge_stage_labels_test.go index 9159b831a7..6875829d7f 100644 --- a/src/pkg/github/automerge/automerge_stage_labels_test.go +++ b/src/pkg/github/automerge/automerge_stage_labels_test.go @@ -52,6 +52,9 @@ type stageCommitFixture struct { authorLogin string authorName string authorEmail string + // parents overrides the default single parent "p"; two entries make + // the commit a merge (served as c, so "c0" names the first commit). + parents []string } func newStageAPI(t *testing.T, api *stageAPI) *httptest.Server { @@ -110,7 +113,14 @@ func newStageAPI(t *testing.T, api *stageAPI) *httptest.Server { if c.authorLogin != "" { author = map[string]string{"login": c.authorLogin} } - out = append(out, map[string]any{"sha": fmt.Sprintf("c%d", i), "author": author, "committer": author, "commit": map[string]any{"message": c.message, "author": map[string]any{"name": c.authorName, "email": c.authorEmail}, "committer": map[string]any{"name": c.authorName, "email": c.authorEmail}}, "parents": []map[string]string{{"sha": "p"}}}) + parents := []map[string]string{{"sha": "p"}} + if len(c.parents) > 0 { + parents = parents[:0] + for _, p := range c.parents { + parents = append(parents, map[string]string{"sha": p}) + } + } + out = append(out, map[string]any{"sha": fmt.Sprintf("c%d", i), "author": author, "committer": author, "commit": map[string]any{"message": c.message, "author": map[string]any{"name": c.authorName, "email": c.authorEmail}, "committer": map[string]any{"name": c.authorName, "email": c.authorEmail}}, "parents": parents}) } } else { for i, msg := range api.commits[n] { diff --git a/src/pkg/stageengine/prstage/types.go b/src/pkg/stageengine/prstage/types.go index ffa75488c6..dda61c7e86 100644 --- a/src/pkg/stageengine/prstage/types.go +++ b/src/pkg/stageengine/prstage/types.go @@ -115,6 +115,9 @@ type Observation struct { Body string // HeadRepo is the head's owner/repo (differs from Display on a fork). HeadRepo string + // HeadRef is the head branch name; with HeadRepo it names the ref an + // in-place DCO repair rewrites (hivecommons/hive#14666). + HeadRef string // ForwardMerge is set for a sync/top-up head between release lines // (head-ref match only: the title is contributor-controlled). ForwardMerge bool diff --git a/src/pkg/stagelabels/store.go b/src/pkg/stagelabels/store.go index 1954ed4df1..64388a0026 100644 --- a/src/pkg/stagelabels/store.go +++ b/src/pkg/stagelabels/store.go @@ -174,6 +174,10 @@ type Record struct { // for; it survives ClearAdoptionEscalation so a blocker that flaps on // one head is noted once. AdoptEscalationNotedHead string `json:"adopt_escalation_noted_head,omitempty"` + // DCORepairHead is the in-place-adopted head whose unsigned hive + // commits the hive last rewrote with its sign-off on the contributor + // branch (hivecommons/hive#14666); one attempt per head. + DCORepairHead string `json:"dco_repair_head,omitempty"` // AdoptRecoveryAttempt is the "@" pair the hive last // tried to recover a stale-red in-place adoption on (update-branch or a // re-run of the failed jobs, hivecommons/hive#14500); one attempt per @@ -923,6 +927,12 @@ func (r Record) AdoptionEscalationNoted(head string) bool { return head != "" && r.AdoptEscalationNotedHead == head } +// DCORepairAttempted reports whether the in-place sign-off rewrite already +// ran for head. +func (r Record) DCORepairAttempted(head string) bool { + return head != "" && r.DCORepairHead == head +} + // AdoptionRecoveryKey is the "@" pair AdoptRecoveryAttempt stores. func AdoptionRecoveryKey(head, base string) string { return head + "@" + base } @@ -1098,6 +1108,21 @@ func (s *Store) MarkAdoptionEscalated(key, head, reason string) { s.dirty = true } +// MarkDCORepair records one in-place sign-off rewrite attempt for head. +// Recorded before the API call, as every adoption path does, so a refused +// rewrite is not retried each sweep. +func (s *Store) MarkDCORepair(key, head string) { + s.mu.Lock() + defer s.mu.Unlock() + r, ok := s.items[key] + if !ok || head == "" || r.DCORepairHead == head { + return + } + r.DCORepairHead = head + s.items[key] = r + s.dirty = true +} + // MarkAdoptionRecovery records one stale-red recovery attempt for head // against base (hivecommons/hive#14500). Recorded before the API call, as // every adoption path does, so a refused call is not retried each sweep. From ddd558981e59665db076de8cdf7690bd61eb2cc4 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Tue, 27 Oct 2026 21:55:37 -0400 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20dco:=20count=20the=20in-plac?= =?UTF-8?q?e=20re-sign=20as=20a=20push,=20not=20a=20comment;=20decision=20?= =?UTF-8?q?test=20covers=20fork=20vs=20base=20head?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/pkg/github/automerge/automerge_contributor_adopt.go | 3 ++- .../github/automerge/automerge_contributor_adopt_test.go | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index 82c7f313f6..a3f2fc1869 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -902,7 +902,8 @@ func (c *Engine) repairAdoptedDCOInPlace(ctx context.Context, store *stagelabels } store.MarkHivePushedHead(key, tip) c.info("stage labels: adopted PR re-signed in place", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "new_head", tip, "branch", obs.HeadRef, "reason", adoptionDCORepaired) - return 1, nil + // A branch push, not a comment: nothing to add to the caller's count. + return 0, nil } // settleAdoption polls the superseding App PR for a PR in stage 074 (at diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index e449c7609a..73d8723b4e 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -521,12 +521,17 @@ func TestAdoptionDCORehomeDecisionAndRendering(t *testing.T) { t.Fatal("unknown unsigned commit ownership must re-home") } obs.Detail.DCOUnsignedHive = 2 + if !adoptionNeedsDCORehome(obs, true) { + t.Fatal("a fork head must re-home even when only hive-authored commits are unsigned: the engine cannot rewrite a fork branch") + } + obs.Owner, obs.Repo, obs.HeadRepo = "acme", "widget", "acme/widget" if adoptionNeedsDCORehome(obs, true) { - t.Fatal("branch push permission with only hive-authored unsigned commits allows in-place repair") + t.Fatal("branch push permission with only hive-authored unsigned commits on a base-repo head allows in-place repair") } if !adoptionNeedsDCORehome(obs, false) { t.Fatal("hive-authored unsigned commits without branch push permission must still re-home") } + obs.HeadRepo = "" inPlace := stagelabels.Record{AdoptedHead: "aaa", AdoptMode: stagelabels.AdoptModeInPlace} if adoptedHeadNeedsDCORehome(obs, inPlace, true) { t.Fatal("already adopted in place with App-owned unsigned commits and push access stays in place") From 43ef1f52aa8741cca14981e97eb9472969fdcbc4 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Tue, 27 Oct 2026 22:06:26 -0400 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9B=20dco:=20decision=20test=20ass?= =?UTF-8?q?erts=20the=20in-place=20repair=20on=20a=20base-repo=20head=20be?= =?UTF-8?q?fore=20the=20fork=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- .../github/automerge/automerge_contributor_adopt_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index 73d8723b4e..8413761a8e 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -531,11 +531,17 @@ func TestAdoptionDCORehomeDecisionAndRendering(t *testing.T) { if !adoptionNeedsDCORehome(obs, false) { t.Fatal("hive-authored unsigned commits without branch push permission must still re-home") } - obs.HeadRepo = "" inPlace := stagelabels.Record{AdoptedHead: "aaa", AdoptMode: stagelabels.AdoptModeInPlace} if adoptedHeadNeedsDCORehome(obs, inPlace, true) { t.Fatal("already adopted in place with App-owned unsigned commits and push access stays in place") } + if !adoptedHeadNeedsDCORepair(obs, inPlace, true) { + t.Fatal("that head is the in-place repair's job") + } + obs.HeadRepo = "" + if !adoptedHeadNeedsDCORehome(obs, inPlace, true) { + t.Fatal("already adopted in place on a fork must re-home") + } if !adoptedHeadNeedsDCORehome(obs, inPlace, false) { t.Fatal("already adopted in place without push access must re-home") } From 6e385de7b9b58123786f7efede49bf25cc988350 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 27 Oct 2026 23:59:23 -0400 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20dco:=20doc=20comment=20on=20?= =?UTF-8?q?repairAdoptedDCOInPlace=20says=20it=20returns=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- src/pkg/github/automerge/automerge_contributor_adopt.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index a3f2fc1869..9b4decacde 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -856,7 +856,8 @@ const adoptionDCORepaired = "dco-rehomed" // force-updates the PR branch to the new tip — once per head, with the ref // re-read first so a push that raced the rewrite is not overwritten // (hivecommons/hive#14666). The next sweep re-reads the new head's commits -// and the DCO gate clears. Returns 1 when the branch moved. +// and the DCO gate clears. Returns 0: a branch push is not a comment, so +// nothing is added to the caller's count. func (c *Engine) repairAdoptedDCOInPlace(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, rec stagelabels.Record) (int, error) { if rec.DCORepairAttempted(obs.HeadSHA) { return 0, nil