From d46c60cbbc2fef964ee29230c0751d44675978f7 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 28 Oct 2026 15:34:59 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Adoption:=20re-adopt=20when=20th?= =?UTF-8?q?e=20hive's=20superseding=20copy=20is=20closed=20unmerged?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A PR adopted in supersede mode whose hive copy was closed without merging was left parked at 085 with "Let hive decide" answering "no change" (hivecommons/hive PR 14947 / copy 14977). - stage-label pass: detect the orphan (settle path and, once per head, a marker scan for PRs at 085 with no adoption on record), re-adopt from the same head with a new hive:adopted marker, revert with hive:adoption-reverted when the head moved or re-adoption fails, and park as adoption-child-closed-twice if copies are closed twice within an hour - merged copy still closes the original the standard way - delegated decision re-adopts instead of no-op - tracker: adopted_by_state closed / closed-twice with plain captions - docs, changelog fragment, tests Fixes #15052 Signed-off-by: Andrew Anderson --- changelog.d/fixed-adoption-orphan-recovery.md | 1 + src/docs/dashboard-sections.md | 4 + src/docs/labels-and-control-signals.md | 34 +- .../api_repo_item_decide_delegated.go | 18 +- .../dashboard/api_repo_item_decide_orphan.go | 92 +++++ .../api_repo_item_decide_orphan_test.go | 120 ++++++ .../dashboard/api_repo_item_decision_test.go | 10 + src/pkg/dashboard/stage_tracker.go | 40 ++ .../stage_tracker_adopted_column.test.js | 15 +- .../stage_tracker_adoption_orphan_test.go | 86 +++++ src/pkg/dashboard/stage_tracker_decision.go | 3 + src/pkg/dashboard/static/index.html | 11 +- .../automerge/automerge_adoption_orphan.go | 297 +++++++++++++++ .../automerge_adoption_orphan_test.go | 343 ++++++++++++++++++ .../automerge/automerge_contributor_adopt.go | 13 +- .../automerge_contributor_adopt_test.go | 38 +- .../automerge/automerge_stage_labels.go | 10 +- src/pkg/stagelabels/adoption_orphan_test.go | 78 ++++ src/pkg/stagelabels/store.go | 67 ++++ 19 files changed, 1241 insertions(+), 39 deletions(-) create mode 100644 changelog.d/fixed-adoption-orphan-recovery.md create mode 100644 src/pkg/dashboard/api_repo_item_decide_orphan.go create mode 100644 src/pkg/dashboard/api_repo_item_decide_orphan_test.go create mode 100644 src/pkg/dashboard/stage_tracker_adoption_orphan_test.go create mode 100644 src/pkg/github/automerge/automerge_adoption_orphan.go create mode 100644 src/pkg/github/automerge/automerge_adoption_orphan_test.go create mode 100644 src/pkg/stagelabels/adoption_orphan_test.go diff --git a/changelog.d/fixed-adoption-orphan-recovery.md b/changelog.d/fixed-adoption-orphan-recovery.md new file mode 100644 index 0000000000..aa00436f1c --- /dev/null +++ b/changelog.d/fixed-adoption-orphan-recovery.md @@ -0,0 +1 @@ +- Adoption: when the hive's superseding copy of an adopted PR is closed without merging, the stage-label pass now re-adopts (fresh copy from the same head, new `hive:adopted` marker), reverts the adoption with a `hive:adoption-reverted` note when the head moved or the copy cannot be opened, and parks the PR as `adoption-child-closed-twice` if copies are closed twice within an hour; "Let hive decide" re-adopts instead of answering "no change", and the tracker shows "Adopted → #N (closed) · recovering" instead of "Needs human decision" (hivecommons/hive#15052). diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index de829790a3..70a3a90d91 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -682,6 +682,8 @@ Fed by `GET /api/stage-tracker`. Every issue and pull request follows the same p **Adoption pill (072).** A nudged `author/clanker` pull request at `stage/072-returned-to-contributor` carries `adoption{author, due, blocked_by, since, nudges, author_online, author_registered, author_last_seen, author_maintainer, policy, action, text}` — the nudge count and first-nudge time from the stage record, the author's live hub presence (the same lookup the contributor-offline adoption itself consults), and, when the adoption engine judged adoption due and held it, why (`blocked_by: author-is-maintainer`). The row's caption and an amber pill say it in plain words instead of a bare "Returned to contributor": `waiting on @hanthor (maintainer, offline 26h) — adopt?` under `governor.labels.stage.adoption.maintainer_policy: ask`, `waiting on @zoe (offline 40m, 2 nudges)` while the default `adopt` policy's clock runs, `… — not adopted: maintainer_policy is never` under `never` ([hivecommons/hive#14524](https://github.com/hivecommons/hive/pull/14524): the PR sat 26h at 072 with its author's hive gone and nothing on screen saying why). When `action` is `decide:adopt` the row also carries `decision_needed` (question "@hanthor maintains this repository and has been offline for 26h; adopt the PR?", `blocker_reason: adoption:author-is-maintainer`, options Adopt then Hold/Release), so the lit 072 chevron and the `adopt?` pill open the Needs-human popover, and the row menu and the selection bar offer **Adopt (hive takes over)** — same action-id vocabulary as the other `decide:*` entries — which, after an in-app confirm, POSTs `{"decision":"adopt"}` to the item's decision endpoint: the hive posts an attributed note, sets `adoption/supersede` (the owner-delegate's request label) and the next sweep adopts the head on the existing adopt/supersede path; the toast reads `adoption-queued`. Write access required; the action is absent when nothing is the owner's to decide. +**Adopted → #N (074 / 085).** An adopted pull request names the hive's superseding copy in its detour word (`adopted_by`). When that copy was closed without merging (hivecommons/hive#15052) the row does not fall back to *Needs human decision*: `adopted_by_state: "closed"` renders "Adopted → #N (closed) · recovering" while the hive opens a fresh copy from the same head, and the caption says so in plain words; once the new copy is on record the word reads "Adopted → #M". If two copies are closed within an hour the hive stops (`adopted_by_state: "closed-twice"`, park reason `adoption-child-closed-twice`): the row reads "Adopted → #N (closed twice) · needs a person" and the popover question asks you to reopen the last copy, give direction, or close the PR. + **Blocked pill.** An issue wearing `blocked` because the hive parked it on a pull request — its body carries `` (the review backlog's spin-off issues, `hive-open-issue comment --blocked-on-pr`) — names that PR: the payload carries `blocked_on_pr: {repo, number, url, state}`, the pill reads `blocked on #N` and links to the PR, its tooltip says `Blocked on PR owner/repo#N (open) — lifts when it merges; re-triage if closed unmerged`, the caption reads `Blocked on PR #N — lifts when it merges` instead of `No stage recorded yet`, and the row's title and chevron tooltip carry the blocker too ([#14952](https://github.com/hivecommons/hive/issues/14952): #14863 showed a bare grey pill and "blocked · since 7h" with nothing to say what it waited on). The state (`open`, `merged`, `closed`, or absent when nothing knows) comes only from what the hive already holds — the status snapshot's open PRs, the ForgeState cache, the recent-merge ring — never a GitHub call from the tracker build; a merged or closed-unmerged blocker says so (`merged; the blocked sweep lifts the label next` / `closed unmerged; the blocked sweep re-triages it next`). The server reads the marker from the issue body when it has one, else from the enumeration's `blocked on owner/repo#N` reason, so sidelined rows (which carry only the reason) name it too. A `blocked` issue with neither (a human-applied label) keeps the bare pill. This is distinct from `blocked_on`, the open GitHub "blocked by" dependencies of a sequenced sub-issue ([#14765](https://github.com/hivecommons/hive/issues/14765)); when a row has both, the chevron tooltip lists them together. **Hive's own unblock is live.** When the blocked sweep or the review-backlog block reconciler lifts `blocked` itself (the tracked PR merged or closed; the `` note), it takes the same path as an inbound label webhook: the tracker cache is dropped and the issue is queued for the targeted re-read (`ItemPatches` delivery `self:edit`), which patches the status row and the governor's cached snapshot at once. A reconcile enumeration that was already listing when the unblock happened used to overwrite that patch with its pre-unblock copy, and the rows reverted to `blocked` until the next pass (~an hour on the hive's hive); the governor now replays re-reads newer than the listing's start onto the pass's result before publishing it, so a row never moves backwards behind a read it has already made. @@ -743,6 +745,8 @@ The glyph is drawn for **every** row on the gate, so two rows in the same state **Setting.** `decisions.delegate.enabled` (default **true** — the same default as the other "let the hive do it" switches); `decisions.delegate.repos..enabled` (or the bare repository name) overrides per repository. `decisions.delegate.llm.enabled` (default **false**) adds the LLM step described below. `decisions.delegate.effect_timeout` (default **30m**, a Go duration) is how long a recorded decision may go without its expected effect before a repeat click reports it as stuck. When the delegate is off the menu entry stays visible but the endpoint answers 403 naming the setting. See [operator-reference.md](operator-reference.md). +**Orphaned adoption.** A PR the hive adopted in supersede mode whose hive copy was closed unmerged is not "already decided": the delegate detects the dead `superseded_by`, re-runs the adoption (a fresh copy from the same head, a new `hive:adopted` marker) and answers *applied* with "Re-adopting: hive's earlier copy #N was closed by mistake; opening a fresh copy." If copies were closed twice within an hour (`adoption-child-closed-twice`) it keeps the park and recommends a person decide rather than opening a third copy. + **What this release decides mechanically.** The decider is rules-first: the cases above that are mechanically decidable (dedupe, owner-only, flaky, CI ratchet, design proposal, review backlog, contributor-offline, sentinel-trusted, small-low-risk, reporter-trust, hive-policy) are decided from the item itself; anything else falls to the reversible default (approve direction on an issue; release a green hive PR; ask for the fix first when the head is red or conflicting). Competing-PR detection and a real adopt-supersede trigger are tracked in a follow-up issue linked from the PR that added this action. **LLM step for the non-mechanical cases.** With `decisions.delegate.llm.enabled: true` (default **false**), an item the rules leave at the reversible default (rule `default-approve` or `default-release`) is shown to one *owner-delegate decider* turn: the item, its comment thread, sentinel findings, linked issues/PRs, the repository's auto-merge setting, the author's association and reporter-trust state, its labels, and `PolicyText` verbatim. The model answers with a structured `{verdict, text, rationale, actions[]}`; the reply is rejected unless every verdict, action kind and label is in the delegate's closed set and fits the item (no `approve-review` on an issue, no action on a park-keeping verdict, …), and unless the actions carry the verdict out: an acting verdict must include its recording action (`comment-decision`, or `close-as-duplicate` for the close verdicts, plus `approve-review` / `adopt-supersede` where the verdict names them), `labels-remove` can never name `stage/085-needs-decision` (only the recording action lifts the park, with the comment and the marker), and `close-duplicate` closes the newer item in favour of an older one. An accepted reply is recorded under rule `llm-delegate` and goes through the same safety rails and executor as a rule decision; a model error or rejected reply keeps the reversible default and the job's `notes` say why. Rules-first stays authoritative: every case a rule decides, and every item wearing `hold`, never reaches the model. The call uses the governor reviewer endpoint (`governor.trajectory`, falling back to `governor.litellm`); `decisions.delegate.llm.model` overrides the model. diff --git a/src/docs/labels-and-control-signals.md b/src/docs/labels-and-control-signals.md index b257624457..8e5dcc5cb4 100644 --- a/src/docs/labels-and-control-signals.md +++ b/src/docs/labels-and-control-signals.md @@ -1091,8 +1091,38 @@ When a clanker-authored contributor PR goes red the hive does not just relabel i its attempt before touching GitHub, so a failed `hive/adopt-` push is not retried on the next sweep. While in 074 the hive polls the superseder at most every 10 minutes: merged → one `` note and the - original is closed; closed unmerged (a maintainer declined) → the adoption - is forgotten and the original returns to 072 (not a regression). + original is closed; closed unmerged → the adoption is **orphaned** and the + hive recovers it rather than leaving the original parked + (hivecommons/hive#15052, where the duplicate-fix sweep closed the hive's + copy and the original sat at 085 with "Let hive decide" answering "no + change"): + - **Re-adopt.** The hive logs `stage labels: adoption orphaned, + re-adopting` (old and new numbers), opens a fresh hive-authored copy + from the same contributor head and posts a new `hive:adopted … mode=supersede + superseded_by=` marker; the original stays 074 and the tracker reads + "Adopted → #old (closed) · recovering" until the new copy is on record, + then "Adopted → #new". + - **Revert.** If the head moved (or the original merged meanwhile) or the + fresh copy could not be opened, the adoption is cleared and the original + returns to its pre-adoption stage with one + `` + comment that says in plain words why. + - **Loop guard.** If the re-adopted copy is itself closed unmerged within + an hour of the first orphan, the hive stops opening copies: it posts + ``, parks the original at + `stage/085-needs-decision` with reason `adoption-child-closed-twice`, and + the tracker shows "Adopted → #N (closed twice) · needs a person". Reopen + the last copy, give direction, or close the original; the park is not + escalated or re-adopted by the hive. + - **Marker scan.** A hive restarted (or rolled) after the orphan happened + has no adoption on record, so once per head a PR at 085 wearing + `author/clanker` has its comments scanned for the last unsettled + `hive:adopted … mode=supersede` marker; a merged copy closes the + original the standard way, a closed one takes the recovery above, an + open one is restored to the record. *Let hive decide* on such a PR does + the same recovery instead of answering "no change" — its outcome reads + "Re-adopting: hive's earlier copy #N was closed by mistake; opening a + fresh copy" (or, past the loop guard, a recommendation to a person). If an adopted head is DCO-red, an empty signed commit is never enough. "Hive-authored" is decided the same way as head attribution above: an unsigned commit is the hive's own only when GitHub links its author or diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated.go b/src/pkg/dashboard/api_repo_item_decide_delegated.go index 181bb97198..ce2cf1d838 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated.go @@ -473,12 +473,23 @@ func (s *Server) decideDelegatedOne(ctx context.Context, repoFull string, number return delegatedOutcome{Err: err} } d, deciderNotes := s.decideDelegated(ctx, in) + // An adoption whose copy was closed unmerged (hivecommons/hive#15052) + // overrides the policy: the recorded decision no longer stands, so the + // answer is to re-adopt, not "already decided". + var orphan orphanedAdoption + if o, ok := s.delegatedOrphanedAdoption(ctx, repoFull, number, in, time.Now()); ok { + orphan = o + d = o.decision("#" + strconv.Itoa(number)) + deciderNotes = append(deciderNotes, fmt.Sprintf("adoption orphaned: hive's copy #%d is closed without merging", o.Dead)) + } // The same rule firing again on an item the hive already decided would // only repeat the comment: report it as a no-op instead of writing — // unless the recorded decision never took effect, in which case say so // in plain words with the options (hivecommons/hive#14822), or the - // operator asked to re-run it. - if prior := s.delegatedPriorDecisionState(ctx, repoFull, number, d.Rule); prior.Found && !req.Force { + // operator asked to re-run it. A re-adoption request older than the + // latest adoption marker was carried out and then orphaned again + // (hivecommons/hive#15052): it is not a repeat. + if prior := s.delegatedPriorDecisionState(ctx, repoFull, number, d.Rule); prior.Found && !req.Force && (orphan.Dead == 0 || prior.At.After(orphan.MarkerAt)) { kind := rowActionTargetIssue if in.Kind == delegatedecision.KindPR { kind = rowActionTargetPR @@ -501,6 +512,9 @@ func (s *Server) decideDelegatedOne(ctx context.Context, repoFull string, number notes, execErr := s.executeDelegatedDecision(ctx, in, d, user, mergerRole) notes = append(deciderNotes, notes...) result := delegatedRowActionResult(in, d, notes) + if orphan.Dead > 0 && !orphan.Looped && execErr == nil { + result.Outcome = delegatedOrphanOutcome(orphan.Dead) + } kinds := make([]string, 0, len(d.Actions)) for _, a := range d.Actions { kinds = append(kinds, string(a.Kind)) diff --git a/src/pkg/dashboard/api_repo_item_decide_orphan.go b/src/pkg/dashboard/api_repo_item_decide_orphan.go new file mode 100644 index 0000000000..a190fabf92 --- /dev/null +++ b/src/pkg/dashboard/api_repo_item_decide_orphan.go @@ -0,0 +1,92 @@ +package dashboard + +import ( + "context" + "fmt" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/delegatedecision" + "github.com/hivecommons/hive/pkg/github/automerge" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// "Let hive decide" on an orphaned adoption (hivecommons/hive#15052). +// +// The delegate's dedup answers a repeat click with "hive already recorded +// this decision (rule contributor-offline)" when the rule's comment is on +// the PR. On hivecommons/hive#14947 that decision had been carried out — +// the hive's copy #14977 opened — and then undone by the duplicate-fix +// sweep closing #14977 unmerged; the recorded decision no longer stood, and +// the answer was wrong. The delegate now reads the PR's adoption markers +// first: a supersede marker whose copy is closed without merging is an +// orphan, and the decision is to re-adopt, under its own rule so the dedup +// keys on the fresh copy rather than the dead one. + +// Rules the orphan path decides under. +const ( + delegatedRuleAdoptionOrphaned = "adoption-orphaned" + delegatedRuleAdoptionLooped = stagelabels.LaneReasonAdoptionChildClosedTwice +) + +// orphanedAdoption is what delegatedOrphanedAdoption found. +type orphanedAdoption struct { + Dead int // the closed, unmerged copy + MarkerAt time.Time // when the adoption that opened it was recorded + Looped bool // copies were closed twice within the hour +} + +// delegatedOrphanedAdoption reports whether the PR wears a live supersede +// marker naming a copy that is closed without merging. Read failures read +// as "not orphaned": the ordinary delegate path then runs. +func (s *Server) delegatedOrphanedAdoption(ctx context.Context, repoFull string, number int, in delegatedecision.Input, now time.Time) (orphanedAdoption, bool) { + if in.Kind != delegatedecision.KindPR || in.HiveAuthored || s.deps == nil || s.deps.GHClient == nil || s.deps.GHClient.GoGitHub() == nil { + return orphanedAdoption{}, false + } + owner, repoName, _ := strings.Cut(repoFull, "/") + api := s.deps.GHClient.GoGitHub() + comments, _, err := api.Issues.ListComments(ctx, owner, repoName, number, &gh.IssueListCommentsOptions{ListOptions: gh.ListOptions{PerPage: delegatedDecisionGatherLimit}}) + if err != nil { + return orphanedAdoption{}, false + } + scan := automerge.ScanAdoptionMarkers(comments) + if !scan.Live { + return orphanedAdoption{}, false + } + sup, _, err := api.PullRequests.Get(ctx, owner, repoName, scan.Marker.SupersededBy) + if err != nil || sup.GetMerged() || !strings.EqualFold(sup.GetState(), "closed") { + return orphanedAdoption{}, false + } + out := orphanedAdoption{Dead: scan.Marker.SupersededBy, MarkerAt: scan.Marker.At, Looped: automerge.AdoptionLoopFromMarkers(scan, now)} + if store := s.stageStoreOrShared(); store != nil { + if rec, ok := store.Get(stagelabels.Key(repoFull, number)); ok && rec.AdoptionChildClosedTwice() { + out.Looped = true + } + } + return out, true +} + +// decision is the delegate's answer for the orphan: re-adopt, or — when +// the copies keep being closed — a recommendation a person must act on. +func (o orphanedAdoption) decision(ref string) delegatedecision.Decision { + if o.Looped { + text := fmt.Sprintf("the hive's copies of %s were closed without merging twice within an hour (last #%d); something keeps closing them, so the hive is not opening a third — reopen a copy, close this PR, or say what should happen", ref, o.Dead) + return delegatedecision.Decision{ + Verdict: delegatedecision.VerdictRecommendOnly, Rule: delegatedRuleAdoptionLooped, Text: text, KeepsPark: true, + Rationale: "a copy closed twice within the hour is a loop the hive must not feed (hivecommons/hive#15052)", + Actions: []delegatedecision.Action{{Kind: delegatedecision.ActionRecommendOnly, Text: text}}, + } + } + text := fmt.Sprintf("re-adopt %s: the hive's earlier copy #%d was closed without merging, so the adoption is re-run from the same head as a fresh copy", ref, o.Dead) + return delegatedecision.Decision{ + Verdict: delegatedecision.VerdictAdoptSupersede, Rule: delegatedRuleAdoptionOrphaned, Text: text, + Rationale: "the recorded supersede decision no longer stands once its copy is closed unmerged (hivecommons/hive#15052)", + Actions: []delegatedecision.Action{{Kind: delegatedecision.ActionCommentDecision, Text: text}, {Kind: delegatedecision.ActionAdoptSupersede, Target: o.Dead}}, + } +} + +// delegatedOrphanOutcome is the row result text for a re-adoption. +func delegatedOrphanOutcome(dead int) string { + return fmt.Sprintf("Re-adopting: hive's earlier copy #%d was closed by mistake; opening a fresh copy.", dead) +} diff --git a/src/pkg/dashboard/api_repo_item_decide_orphan_test.go b/src/pkg/dashboard/api_repo_item_decide_orphan_test.go new file mode 100644 index 0000000000..b101af25e0 --- /dev/null +++ b/src/pkg/dashboard/api_repo_item_decide_orphan_test.go @@ -0,0 +1,120 @@ +package dashboard + +import ( + "net/http" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/delegatedecision" + "github.com/hivecommons/hive/pkg/github/automerge" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// hivecommons/hive#15052: "Let hive decide" on an adoption whose hive copy +// was closed unmerged. + +const orphanAdoptedMarker = automerge.AdoptionMarkerPrefix + " by=h1 reason=contributor-offline mode=supersede superseded_by=90 head=feedfacefeedfacefeedfacefeedfacefeedface -->\n@someone the hive has adopted this PR." + +func orphanPriorDecision() string { + return delegatedecision.CommentBody(delegatedecision.Decision{Verdict: delegatedecision.VerdictAdoptSupersede, Rule: "contributor-offline", Rationale: "r"}, "h1", "writer") +} + +// The recorded contributor-offline decision has a closed, unmerged copy +// behind it: not "no change" but a fresh adoption request, said plainly. +func TestDelegatedDecisionOrphanedAdoptionReadopts(t *testing.T) { + mock := &repoItemDecisionMock{prForeign: true, prRedCI: true, prMergeState: "blocked", supersederState: "closed"} + mock.priorComments = []string{orphanPriorDecision(), orphanAdoptedMarker} + srv := newDelegatedServer(t, mock) + w := postDelegated(t, srv, 8, "writer", `{"type":"pr","reason":"contributor-lane:decision-requested","caption":"Needs human decision"}`, nil) + if w.Code != http.StatusOK { + t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) + } + job := decodeDelegatedJob(t, w) + if job.Result == nil || job.Result.Action != rowActionAdopted { + t.Fatalf("result = %+v status=%s err=%s", job.Result, job.Status, job.Error) + } + if job.Result.Outcome != "Re-adopting: hive's earlier copy #90 was closed by mistake; opening a fresh copy." { + t.Fatalf("outcome = %q", job.Result.Outcome) + } + if job.Decision == nil || job.Decision.Rule != delegatedRuleAdoptionOrphaned || job.Decision.Verdict != delegatedecision.VerdictAdoptSupersede { + t.Fatalf("decision = %+v", job.Decision) + } + if !strings.Contains(strings.Join(mock.added, ","), delegatedecision.LabelAdoptSupersede) { + t.Fatalf("re-adoption not queued: %v", mock.added) + } + if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "closed without merging") || !strings.Contains(mock.comments[0], "#90") { + t.Fatalf("decision comment: %v", mock.comments) + } + if len(mock.edits) != 0 { + t.Fatalf("original PR edited: %v", mock.edits) + } +} + +// A copy still open, or merged, is not an orphan: the recorded decision +// stands and a repeat click is the usual no-op (or, past the effect +// timeout, the plain-words "hasn't taken effect" of hivecommons/hive#14822) +// — never a re-adoption. +func TestDelegatedDecisionAdoptionWithLiveCopyIsNoop(t *testing.T) { + for _, tc := range []struct { + name string + state string + merged bool + }{{"open", "open", false}, {"merged", "closed", true}} { + t.Run(tc.name, func(t *testing.T) { + mock := &repoItemDecisionMock{prForeign: true, prRedCI: true, prMergeState: "blocked", supersederState: tc.state, supersederMerged: tc.merged} + mock.priorComments = []string{orphanPriorDecision(), orphanAdoptedMarker} + srv := newDelegatedServer(t, mock) + w := postDelegated(t, srv, 8, "writer", `{"type":"pr","reason":"contributor-lane:decision-requested"}`, nil) + job := decodeDelegatedJob(t, w) + if job.Result == nil || (job.Result.Action != rowActionNoop && job.Result.Action != rowActionNoEffect) || job.Result.Rule != "contributor-offline" { + t.Fatalf("result = %+v", job.Result) + } + if job.Decision == nil || job.Decision.Rule == delegatedRuleAdoptionOrphaned || strings.Contains(job.Result.Outcome, "Re-adopting") { + t.Fatalf("re-adopted a standing adoption: decision=%+v result=%+v", job.Decision, job.Result) + } + if len(mock.added) != 0 || strings.Contains(strings.Join(mock.comments, ","), "closed without merging") { + t.Fatalf("wrote on a standing adoption: comments=%v added=%v", mock.comments, mock.added) + } + }) + } +} + +// The store says the copies were closed twice within the hour: no third +// copy; a recommendation names the loop and leaves the park. +func TestDelegatedDecisionOrphanedAdoptionLoopedRecommends(t *testing.T) { + mock := &repoItemDecisionMock{prForeign: true, prRedCI: true, prMergeState: "blocked", supersederState: "closed"} + mock.priorComments = []string{orphanPriorDecision(), orphanAdoptedMarker} + srv := newDelegatedServer(t, mock) + store := stagelabels.NewStore("", nil) + key := stagelabels.Key("testorg/testrepo", 8) + now := time.Date(2026, 10, 28, 18, 0, 0, 0, time.UTC) + store.Observe(key, stagelabels.NeedsDecision, stagelabels.LaneReasonAdoptionChildClosedTwice) + store.MarkAdopted(key, "feedfacefeedfacefeedfacefeedfacefeedface", 90, now) + store.MarkAdoptionEscalated(key, "feedfacefeedfacefeedfacefeedfacefeedface", stagelabels.LaneReasonAdoptionChildClosedTwice) + srv.stageStore = store + w := postDelegated(t, srv, 8, "writer", `{"type":"pr","reason":"adoption-child-closed-twice"}`, nil) + job := decodeDelegatedJob(t, w) + if job.Result == nil || job.Result.Action != rowActionRecommended || !strings.Contains(job.Result.Outcome, "twice within an hour") { + t.Fatalf("result = %+v", job.Result) + } + if strings.Contains(strings.Join(mock.added, ","), delegatedecision.LabelAdoptSupersede) { + t.Fatalf("a third copy was requested: %v", mock.added) + } + if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "not opening a third") { + t.Fatalf("recommendation: %v", mock.comments) + } +} + +func TestOrphanedAdoptionDecisionText(t *testing.T) { + d := orphanedAdoption{Dead: 14977}.decision("#14947") + if d.Rule != delegatedRuleAdoptionOrphaned || len(d.Actions) != 2 || d.Actions[1].Kind != delegatedecision.ActionAdoptSupersede || d.Actions[1].Target != 14977 { + t.Fatalf("decision = %+v", d) + } + if !strings.Contains(d.Text, "earlier copy #14977 was closed without merging") { + t.Fatalf("text = %q", d.Text) + } + if got := delegatedOrphanOutcome(14977); got != "Re-adopting: hive's earlier copy #14977 was closed by mistake; opening a fresh copy." { + t.Fatalf("outcome = %q", got) + } +} diff --git a/src/pkg/dashboard/api_repo_item_decision_test.go b/src/pkg/dashboard/api_repo_item_decision_test.go index ee485e1bcd..2d4cba6eb6 100644 --- a/src/pkg/dashboard/api_repo_item_decision_test.go +++ b/src/pkg/dashboard/api_repo_item_decision_test.go @@ -38,6 +38,10 @@ type repoItemDecisionMock struct { // priorComments are the comment bodies GET /comments lists (a prior // delegated decision makes a repeat click a no-op). priorComments []string + // supersederState / supersederMerged shape GET pulls/90, the hive copy + // an adoption marker on #8 may name (hivecommons/hive#15052); "" is 404. + supersederState string + supersederMerged bool } func newRepoItemDecisionServer(t *testing.T, m *repoItemDecisionMock) *Server { @@ -64,6 +68,12 @@ func newRepoItemDecisionServer(t *testing.T, m *repoItemDecisionMock) *Server { n = 9 } _ = json.NewEncoder(w).Encode(map[string]any{"number": n, "state": "open", "labels": []any{}, "pull_request": map[string]any{"url": "x"}, "user": map[string]any{"login": "hive[bot]", "type": "Bot"}}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/pulls/90"): + if m.supersederState == "" { + http.NotFound(w, r) + return + } + _ = json.NewEncoder(w).Encode(map[string]any{"number": 90, "state": m.supersederState, "merged": m.supersederMerged, "user": map[string]any{"login": "hive[bot]", "type": "Bot"}, "head": map[string]any{"sha": "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef"}}) case r.Method == http.MethodGet && (strings.HasSuffix(path, "/pulls/8") || strings.HasSuffix(path, "/pulls/9")): // PR #9 is a second hive PR nested under issue #7 (the per-PR // delegated decision); it reads like #8. diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index c3d87fdd34..32863dd114 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -402,6 +402,11 @@ type StageTrackerItem struct { NudgeCount int `json:"nudge_count,omitempty"` LastNudgeAt *time.Time `json:"last_nudge_at,omitempty"` AdoptedBy int `json:"adopted_by,omitempty"` + // AdoptedByState qualifies AdoptedBy when the hive's copy is not simply + // open (hivecommons/hive#15052): "closed" — the copy was closed without + // merging and the hive is re-adopting; "closed-twice" — two copies were + // closed within the hour, so the hive stopped and a person must decide. + AdoptedByState string `json:"adopted_by_state,omitempty"` // Adoption is, on a nudged PR at stage/072, whether adoption is due, // what holds it, and the author's live presence, with the plain-words // line the row shows instead of a bare "Returned to contributor" @@ -1136,6 +1141,8 @@ func humanizeStageReason(reason string) string { return "the PR references it without closing it (partial)" } switch lower { + case stagelabels.LaneReasonAdoptionChildClosedTwice: + return "the hive's copies of this PR were closed without merging twice within an hour" case "held": return "held by a hold label" case "label:needs-rebase": @@ -1413,12 +1420,18 @@ func stageTrackerCaption(stage stagelabels.Stage, reason, author string, kind st case stagelabels.AdoptedByHive: c := adoptedWorkCaption(reason) if hasRec && rec.AdoptedBy > 0 { + if stageTrackerAdoptedByState(rec) == stageTrackerAdoptedClosed { + return joinClause("The hive's copy #"+strconv.Itoa(rec.AdoptedBy)+" was closed without merging; the hive is opening a fresh copy from the same head", clause), "hive" + } c += " (superseded by #" + strconv.Itoa(rec.AdoptedBy) + ")" } return joinClause(c, clause), "hive" case stagelabels.AwaitingMergeAdmission: return joinClause("Green and approved; waiting for the automerge lane to admit it", clause), "automerge lane" case stagelabels.NeedsDecision, stagelabels.IssueNeedsDecision: + if hasRec && stageTrackerAdoptedByState(rec) == stageTrackerAdoptedClosedTwice { + return stageTrackerAdoptionLoopCaption(rec), "maintainer" + } if step, _ := stageTrackerUnauthorized(reason); step != "" { // hivecommons/hive#14589: not a decision but a step only a // person may take; the gate cell says the same. @@ -1504,6 +1517,32 @@ func timePtr(t time.Time) *time.Time { return &u } +// AdoptedByState values (StageTrackerItem.AdoptedByState). +const ( + stageTrackerAdoptedClosed = "closed" + stageTrackerAdoptedClosedTwice = "closed-twice" +) + +// stageTrackerAdoptedByState reads the copy's state from the record +// (hivecommons/hive#15052): the loop-guard park, else a copy the hive noted +// closed and has not yet replaced. +func stageTrackerAdoptedByState(rec stagelabels.Record) string { + switch { + case rec.AdoptedBy <= 0: + return "" + case rec.AdoptionChildClosedTwice(): + return stageTrackerAdoptedClosedTwice + case rec.AdoptOrphanedBy == rec.AdoptedBy: + return stageTrackerAdoptedClosed + } + return "" +} + +// stageTrackerAdoptionLoopCaption is the 085 caption for the loop guard. +func stageTrackerAdoptionLoopCaption(rec stagelabels.Record) string { + return "The hive's copies of this PR (last #" + strconv.Itoa(rec.AdoptedBy) + ") were closed without merging twice within an hour; the hive stopped opening copies and a person must decide" +} + // stageStoreOrShared returns the injected stage store (tests) or the // process-wide one the reconcilers write. func (s *Server) stageStoreOrShared() *stagelabels.Store { @@ -1618,6 +1657,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto it.LastNudgeAt = &t2 } it.AdoptedBy = rec.AdoptedBy + it.AdoptedByState = stageTrackerAdoptedByState(rec) it.SoakRemainingSeconds = int64(stageTrackerSoakRemaining(rec, now) / time.Second) } it.Hold = stageTrackerHoldFrom(rec, hasRec, labels) diff --git a/src/pkg/dashboard/stage_tracker_adopted_column.test.js b/src/pkg/dashboard/stage_tracker_adopted_column.test.js index dd0d02bba3..d5074090ce 100644 --- a/src/pkg/dashboard/stage_tracker_adopted_column.test.js +++ b/src/pkg/dashboard/stage_tracker_adopted_column.test.js @@ -96,6 +96,17 @@ test('adopted word names the superseding PR and is empty otherwise', () => { assert.strictEqual(lifted.adoptedWord(null), ''); }); -test('the detour word for 074 uses the adopted pill in the shipped renderer', () => { - assert.match(html, /cur === STAGE_TRACKER_ADOPTED_BY_HIVE && stageTrackerAdoptedWord\(item\)/); +// hivecommons/hive#15052: a copy closed unmerged says so and that the hive +// is re-adopting; the loop guard names the park. The 085 row carrying +// either state uses the adopted word, not "Needs human decision". +test('adopted word qualifies a closed copy and the loop-guard park', () => { + assert.strictEqual(lifted.adoptedWord({ stage: 74, adopted_by: 14977, adopted_by_state: 'closed' }), 'Adopted → #14977 (closed) · recovering'); + assert.strictEqual(lifted.adoptedWord({ stage: 85, adopted_by: 14977, adopted_by_state: 'closed' }), 'Adopted → #14977 (closed) · recovering'); + assert.strictEqual(lifted.adoptedWord({ stage: 85, adopted_by: 15060, adopted_by_state: 'closed-twice' }), 'Adopted → #15060 (closed twice) · needs a person'); + assert.strictEqual(lifted.adoptedWord({ stage: 74, adopted_by: 15060, adopted_by_state: '' }), 'Adopted → #15060'); + assert.strictEqual(lifted.adoptedWord({ stage: 85, adopted_by_state: 'closed' }), ''); +}); + +test('the detour word for 074 (and a qualified 085) uses the adopted pill in the shipped renderer', () => { + assert.match(html, /\(cur === STAGE_TRACKER_ADOPTED_BY_HIVE \|\| \(cur === STAGE_TRACKER_HUMAN_GATED && item\.adopted_by_state\)\) && stageTrackerAdoptedWord\(item\)/); }); diff --git a/src/pkg/dashboard/stage_tracker_adoption_orphan_test.go b/src/pkg/dashboard/stage_tracker_adoption_orphan_test.go new file mode 100644 index 0000000000..592a4ce3b7 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_adoption_orphan_test.go @@ -0,0 +1,86 @@ +package dashboard + +import ( + "encoding/json" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// hivecommons/hive#15052: an adopted PR whose hive copy was closed unmerged +// reads "Adopted → #N (closed) · recovering" rather than "Needs human +// decision"; after re-adoption it names the fresh copy; the loop guard +// park says plainly why a person must decide. + +func orphanTrackerFixture(t *testing.T, now time.Time, stage stagelabels.Stage, reason string, labels []string) (*Server, *stagelabels.Store, *StatusPayload, string) { + t.Helper() + s := newTestServer() + store := stagelabels.NewStore("", func() time.Time { return now }) + s.stageStore = store + s.deps = &Dependencies{Config: &config.Config{}} + key := stagelabels.Key("hivecommons/hive", 14947) + store.Observe(key, stage, reason) + status := minimalPayload() + status.HiveID = "hive" + status.Repos = []FrontendRepo{{ + Name: "hive", Full: "hivecommons/hive", + OpenPrs: []any{FrontendPR{PullRequest: github.PullRequest{Number: 14947, Title: "Fix", Author: "thatguywob", HeadSHA: "bc3a6ea", Labels: append([]string{"author/clanker", "role/contributor"}, labels...)}}}, + }} + s.UpdateStatus(status) + return s, store, status, key +} + +func TestStageTrackerAdoptionOrphanedShowsRecovering(t *testing.T) { + now := time.Date(2026, 10, 28, 16, 0, 0, 0, time.UTC) + s, store, status, key := orphanTrackerFixture(t, now, stagelabels.AdoptedByHive, "contributor-lane:adopted", []string{"stage/074-adopted-by-hive"}) + store.MarkAdopted(key, "bc3a6ea", 14977, now.Add(-20*time.Minute)) + store.NoteAdoptionOrphaned(key, 14977, now) + + it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 14947) + if it.AdoptedBy != 14977 || it.AdoptedByState != "closed" { + t.Fatalf("adopted_by=%d state=%q", it.AdoptedBy, it.AdoptedByState) + } + if !strings.Contains(it.Caption, "copy #14977 was closed without merging") || !strings.Contains(it.Caption, "fresh copy") { + t.Fatalf("caption = %q", it.Caption) + } + raw, _ := json.Marshal(it) + if !strings.Contains(string(raw), `"adopted_by_state":"closed"`) { + t.Fatalf("payload lacks adopted_by_state: %s", raw) + } + + // Re-adopted: the fresh copy is on record and the state clears. + store.MarkAdopted(key, "bc3a6ea", 15060, now) + it = trackerItem(t, s.buildStageTracker(status, store, now), "pr", 14947) + if it.AdoptedBy != 15060 || it.AdoptedByState != "" { + t.Fatalf("after re-adopt adopted_by=%d state=%q", it.AdoptedBy, it.AdoptedByState) + } + if !strings.Contains(it.Caption, "superseded by #15060") { + t.Fatalf("caption after re-adopt = %q", it.Caption) + } +} + +func TestStageTrackerAdoptionClosedTwiceSaysWhy(t *testing.T) { + now := time.Date(2026, 10, 28, 16, 0, 0, 0, time.UTC) + s, store, status, key := orphanTrackerFixture(t, now, stagelabels.NeedsDecision, stagelabels.LaneReasonAdoptionChildClosedTwice, []string{"stage/085-needs-decision"}) + store.MarkAdopted(key, "bc3a6ea", 15060, now.Add(-10*time.Minute)) + store.NoteAdoptionOrphaned(key, 15060, now) + store.MarkAdoptionEscalated(key, "bc3a6ea", stagelabels.LaneReasonAdoptionChildClosedTwice) + + it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 14947) + if it.AdoptedBy != 15060 || it.AdoptedByState != "closed-twice" { + t.Fatalf("adopted_by=%d state=%q", it.AdoptedBy, it.AdoptedByState) + } + if !strings.Contains(it.Caption, "closed without merging twice within an hour") || !strings.Contains(it.Caption, "a person must decide") { + t.Fatalf("caption = %q", it.Caption) + } + if it.DecisionNeeded == nil || !strings.Contains(it.DecisionNeeded.Question, "closed without merging twice within an hour") || !strings.Contains(it.DecisionNeeded.Question, "#15060") { + t.Fatalf("decision_needed = %+v", it.DecisionNeeded) + } + if got := humanizeStageReason(stagelabels.LaneReasonAdoptionChildClosedTwice); !strings.Contains(got, "twice within an hour") { + t.Fatalf("humanizeStageReason = %q", got) + } +} diff --git a/src/pkg/dashboard/stage_tracker_decision.go b/src/pkg/dashboard/stage_tracker_decision.go index ea56d52d1f..f2ad92bc7c 100644 --- a/src/pkg/dashboard/stage_tracker_decision.go +++ b/src/pkg/dashboard/stage_tracker_decision.go @@ -2,6 +2,7 @@ package dashboard import ( "slices" + "strconv" "strings" "time" @@ -341,6 +342,8 @@ func decisionForPR(it StageTrackerItem, verdict *review.Aggregate, sentinelLabel reason = "security-relevant changes" } d.Question = "The security check flagged " + reason + at + ". Accept it as is, give direction, or close it?" + case lower == stagelabels.LaneReasonAdoptionChildClosedTwice: + d.Question = "The hive's copies of this PR were closed without merging twice within an hour, so the hive stopped opening copies. Reopen the last copy (#" + strconv.Itoa(it.AdoptedBy) + "), give direction, or close it?" case adoptionStalled: d.Question = "The hive took this PR over but could not move it (" + decisionPlainReason(adoptionLane) + "). Give direction, or close it?" case forkGuarded: diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 33140c3241..2548676deb 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -13942,9 +13942,16 @@

Where to get help

// PR), so it renders in the CI/Review column with the adoptee named — // never "Needs human decision" (hivecommons/hive#14975 → #14980). const STAGE_TRACKER_ADOPTED_BY_HIVE = 74; + // adopted_by_state (hivecommons/hive#15052): "closed" — the copy was + // closed unmerged and the hive is re-adopting; "closed-twice" — the + // loop guard parked the PR for a person. function stageTrackerAdoptedWord(item) { const by = Number(item && item.adopted_by) || 0; - return by > 0 ? 'Adopted → #' + by : ''; + if (by <= 0) return ''; + const state = String((item && item.adopted_by_state) || ''); + if (state === 'closed') return 'Adopted → #' + by + ' (closed) · recovering'; + if (state === 'closed-twice') return 'Adopted → #' + by + ' (closed twice) · needs a person'; + return 'Adopted → #' + by; } const STAGE_TRACKER_UNAUTHORIZED_WORD = 'Awaiting human'; function stageTrackerUnauthorizedWord(item) { @@ -14408,7 +14415,7 @@

Where to get help

const terminal = anchor >= STAGE_TRACKER_TERMINAL; const sentinelLane = stageTrackerSentinelInLane(item); const detour = (!terminal && STAGE_TRACKER_PATH.indexOf(cur) < 0 && cur !== STAGE_TRACKER_TRIAGE_NEEDED && cur !== STAGE_TRACKER_POSSIBLY_EXISTS && cur !== 40 && cur !== 60 && cur !== STAGE_TRACKER_PRE_MERGE_SOAK && cur !== 100) - ? { text: sentinelLane ? STAGE_TRACKER_SENTINEL_WORD : (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && item.manual_merge ? stageTrackerManualMergeWord(item) : (cur === STAGE_TRACKER_HUMAN_GATED && stageTrackerUnauthorizedWord(item)) || (cur === STAGE_TRACKER_ADOPTED_BY_HIVE && stageTrackerAdoptedWord(item)) || (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && stageTrackerReadyForHumanWord(item, STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))) || (STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))), sentinel: sentinelLane } : null; + ? { text: sentinelLane ? STAGE_TRACKER_SENTINEL_WORD : (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && item.manual_merge ? stageTrackerManualMergeWord(item) : (cur === STAGE_TRACKER_HUMAN_GATED && stageTrackerUnauthorizedWord(item)) || ((cur === STAGE_TRACKER_ADOPTED_BY_HIVE || (cur === STAGE_TRACKER_HUMAN_GATED && item.adopted_by_state)) && stageTrackerAdoptedWord(item)) || (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && stageTrackerReadyForHumanWord(item, STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))) || (STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))), sentinel: sentinelLane } : null; const segs = STAGE_TRACKER_PATH.map(n => { const st = n === 0 ? { label: 'Open', short: 'Open', description: 'Open, not yet in a stage' } : (byNum[n] || {}); let state = 'todo'; diff --git a/src/pkg/github/automerge/automerge_adoption_orphan.go b/src/pkg/github/automerge/automerge_adoption_orphan.go new file mode 100644 index 0000000000..6a7e4eae13 --- /dev/null +++ b/src/pkg/github/automerge/automerge_adoption_orphan.go @@ -0,0 +1,297 @@ +package automerge + +import ( + "context" + "fmt" + "regexp" + "strconv" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/authorkind" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Orphaned adoptions (hivecommons/hive#15052). +// +// A supersede-mode adoption hands the contributor's head to a hive-authored +// copy and parks the original at 074 until that copy merges. When the copy +// is closed without merging — the duplicate-fix sweep closed #14977 under +// #14947 six minutes after the adoption — the original used to forget the +// adoption and fall back to whatever stage its lane derived. On #14947 that +// was 085 (reviewer verdict requires_human), where the 072 adoption +// machinery never runs: the PR sat with "Needs human decision" while the +// hive believed the question was answered on a PR that no longer existed. +// +// The reconciler now treats a closed-unmerged copy as an orphan and +// re-adopts from the same head: a fresh copy, a fresh marker, one log line +// naming the old and new numbers. When the head moved or the fresh copy +// cannot be opened, the adoption is reverted with a plain note so the PR +// rejoins its normal flow instead of waiting on nothing. A copy closed again +// within stagelabels.AdoptionOrphanLoopWindow is a loop: the PR parks at 085 +// with LaneReasonAdoptionChildClosedTwice and no third copy is opened. +// +// Detection has two sources. The store path runs where the superseder is +// polled (settleSuperseder) and covers every adoption on record. The marker +// path covers a record that already forgot its adoption (every store written +// before this change, or one rebuilt from scratch): once per head, a PR at +// 085 whose author is a clanker has its comments read for the last +// `hive:adopted … mode=supersede superseded_by=N` marker, and the copy's +// state settles it the same way. + +// AdoptionRevertedMarker opens the note posted when an orphaned adoption +// cannot be re-run and the PR returns to its pre-adoption flow. +const AdoptionRevertedMarker = "") + if end < 0 { + return AdoptionMarker{}, false + } + head := body[:end] + if m := adoptionMarkerModeRE.FindStringSubmatch(head); m == nil || m[1] != stagelabels.AdoptModeSupersede { + return AdoptionMarker{}, false + } + m := adoptionMarkerSupersededByRE.FindStringSubmatch(head) + if m == nil { + return AdoptionMarker{}, false + } + n, err := strconv.Atoi(m[1]) + if err != nil || n <= 0 { + return AdoptionMarker{}, false + } + out := AdoptionMarker{SupersededBy: n} + if h := adoptionMarkerHeadRE.FindStringSubmatch(head); h != nil { + out.Head = h[1] + } + return out, true +} + +// AdoptionSettledMarker reports a comment that ends the adoption the +// marker before it opened: the copy merged and the original was told, the +// adoption was reverted or loop-parked, yielded, or released. +func AdoptionSettledMarker(body string) bool { + body = strings.TrimSpace(body) + for _, prefix := range []string{adoptionCloseMarker, AdoptionRevertedMarker, AdoptionChildClosedTwiceMarker, AdoptionYieldMarker, AdoptionSupersederMergedDivergedMarker, AdoptionMaintainerSupersederMarker} { + if strings.HasPrefix(body, prefix) { + return true + } + } + return false +} + +// AdoptionMarkerScan is what ScanAdoptionMarkers found: the last live +// supersede marker (Live false when none, or a later note settled it) and +// how many supersede markers the PR carries in all. +type AdoptionMarkerScan struct { + Live bool + Marker AdoptionMarker + Count int +} + +// ScanAdoptionMarkers reads the comments in posting order. +func ScanAdoptionMarkers(comments []*gh.IssueComment) AdoptionMarkerScan { + var out AdoptionMarkerScan + for _, c := range comments { + if c == nil { + continue + } + body := c.GetBody() + if m, ok := ParseAdoptionMarker(body); ok { + m.At = c.GetCreatedAt().Time + out.Marker, out.Live = m, true + out.Count++ + continue + } + if out.Live && AdoptionSettledMarker(body) { + out.Live = false + } + } + return out +} + +// AdoptionLoopFromMarkers reports whether the marker history alone shows +// the loop guard's condition: more than one supersede marker, the latest +// within stagelabels.AdoptionOrphanLoopWindow of now. +func AdoptionLoopFromMarkers(scan AdoptionMarkerScan, now time.Time) bool { + return scan.Live && scan.Count > 1 && now.Sub(scan.Marker.At) < stagelabels.AdoptionOrphanLoopWindow +} + +// AdoptionRevertedComment is the note left when an orphaned adoption is +// handed back to the PR's normal flow. +func AdoptionRevertedComment(obs stageObservation, dead int, why string) string { + var cause string + switch why { + case adoptionRevertHeadMoved: + cause = fmt.Sprintf("this PR has moved to a newer commit (`%s`) since, so the hive is not copying the old one again", shortSHA(obs.HeadSHA)) + default: + cause = "the hive could not open a fresh copy just now" + } + return fmt.Sprintf("%s superseded_by=%d head=%s why=%s -->\n@%s the hive's copy of this PR, #%d, was closed without merging, and %s. "+ + "The adoption is undone: this PR is back in its normal flow (`%s`), and the hive nudges or adopts again from here if CI stays red.", + AdoptionRevertedMarker, dead, obs.HeadSHA, why, obs.Author, dead, cause, stagelabels.ReturnedToContributor.Label()) +} + +// AdoptionChildClosedTwiceComment is the loop-guard note. +func AdoptionChildClosedTwiceComment(obs stageObservation, first, second int) string { + return fmt.Sprintf("%s first=%d second=%d head=%s -->\nThe hive's copies of this PR were closed without merging twice within an hour (#%d, then #%d). "+ + "Something keeps closing them, so the hive is not opening a third. This PR needs a person (`%s`): reopen one of the copies, close this PR, or say what should happen.", + AdoptionChildClosedTwiceMarker, first, second, obs.HeadSHA, first, second, stagelabels.NeedsDecision.Label()) +} + +// recoverOrphanedAdoption runs where the superseder poll found the copy +// closed unmerged. rec carries the adoption (AdoptedBy is the dead copy). +// It returns the comments posted. +func (c *Engine) recoverOrphanedAdoption(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, rec stagelabels.Record, policy StageLabelPolicy, now time.Time) (int, error) { + dead := rec.AdoptedBy + if dead <= 0 { + return 0, nil + } + logArgs := func(extra ...any) []any { + return append([]any{"repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "adopted_head", rec.AdoptedHead, "superseded_by", dead}, extra...) + } + if rec.AdoptionOrphanedWithin(now, stagelabels.AdoptionOrphanLoopWindow) && rec.AdoptOrphanedBy != dead { + // The fresh copy from the last recovery is closed too: stop here. + first := rec.AdoptOrphanedBy + store.NoteAdoptionOrphaned(key, dead, now) + store.MarkAdoptionEscalated(key, rec.AdoptedHead, stagelabels.LaneReasonAdoptionChildClosedTwice) + c.warn("stage labels: adoption copy closed twice within the hour; parking for a person", logArgs("first", first, "reason", stagelabels.LaneReasonAdoptionChildClosedTwice)...) + if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(AdoptionChildClosedTwiceComment(obs, first, dead))}); err != nil { + c.warn("stage labels: adoption loop-guard note failed", logArgs("error", err)...) + return 0, err + } + return 1, nil + } + if obs.HeadSHA == "" || obs.HeadSHA != rec.AdoptedHead || obs.Merged { + c.warn("stage labels: adoption orphaned but the head moved; reverting adoption", logArgs("why", adoptionRevertHeadMoved)...) + return c.revertOrphanedAdoption(ctx, store, key, obs, dead, adoptionRevertHeadMoved) + } + store.NoteAdoptionOrphaned(key, dead, now) + c.info("stage labels: adoption orphaned, re-adopting", logArgs("old", dead, "attempt", rec.AdoptOrphanCount+1)...) + // supersedeAdoptedPR records the fresh copy on the store; the dead + // number stays only when it could not open one. + reason := nonEmpty(rec.AdoptReason, AdoptReasonOffline) + n, err := c.supersedeAdoptedPR(ctx, store, key, obs, rec, policy, reason, adoptionGrace(rec, policy), rec.NudgeOnline, false, now) + after, _ := store.Get(key) + if after.AdoptedBy > 0 && after.AdoptedBy != dead { + c.info("stage labels: adoption re-adopted", logArgs("old", dead, "new", after.AdoptedBy)...) + return n, err + } + // The failed attempt is logged, not returned: the revert is its + // resolution, and the sweep must not report an error for a PR that is + // now back in its normal flow. + c.warn("stage labels: re-adoption did not open a fresh copy; reverting adoption", logArgs("why", adoptionRevertReadoptFail, "error", err)...) + m, rerr := c.revertOrphanedAdoption(ctx, store, key, obs, dead, adoptionRevertReadoptFail) + return n + m, rerr +} + +// revertOrphanedAdoption forgets the adoption and tells the PR why. +func (c *Engine) revertOrphanedAdoption(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, dead int, why string) (int, error) { + store.ClearAdoption(key) + if obs.Merged { + return 0, nil + } + if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(AdoptionRevertedComment(obs, dead, why))}); err != nil { + c.warn("stage labels: adoption revert note failed", "repo", obs.Display, "pr", obs.Number, "superseded_by", dead, "error", err) + return 0, err + } + return 1, nil +} + +// adoptionMarkerScanDue reports a PR whose record carries no adoption but +// may still wear a supersede marker on GitHub: a clanker's open PR the lane +// parks at 085, not yet read at this head. +func adoptionMarkerScanDue(obs stageObservation, rec stagelabels.Record, hasRec bool, stage stagelabels.Stage, policy StageLabelPolicy) bool { + if !policy.Adoption || !hasRec || obs.Merged || obs.HeadSHA == "" || obs.Lane == stageLaneSelf { + return false + } + if stage != stagelabels.NeedsDecision || obs.AuthorKind != authorkind.Clanker { + return false + } + if rec.AdoptedBy > 0 || rec.AdoptionLive(obs.HeadSHA) || rec.Superseded() { + return false + } + return rec.AdoptMarkerScanHead != obs.HeadSHA +} + +// recoverOrphanedAdoptionFromMarker reads the PR's comments once per head +// for a supersede marker the store forgot and settles it from the copy's +// state: merged → the close path, closed → the orphan recovery, open → the +// adoption is put back on record. +func (c *Engine) recoverOrphanedAdoptionFromMarker(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, policy StageLabelPolicy, now time.Time) (int, error) { + store.MarkAdoptMarkerScanned(key, obs.HeadSHA) + comments, err := c.listIssueComments(ctx, obs.Owner, obs.Repo, obs.Number) + if err != nil { + c.warn("stage labels: adoption marker scan failed", "repo", obs.Display, "pr", obs.Number, "error", err) + return 0, err + } + scan := ScanAdoptionMarkers(comments) + if !scan.Live { + return 0, nil + } + dead := scan.Marker.SupersededBy + logArgs := []any{"repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "marker_head", scan.Marker.Head, "superseded_by", dead, "markers", scan.Count} + sup, _, err := c.gh.PullRequests.Get(ctx, obs.Owner, obs.Repo, dead) + if err != nil { + c.warn("stage labels: adoption marker names a copy the hive cannot read", append(logArgs, "error", err)...) + return 0, err + } + // The marker is the adoption the store lost: restore it so every + // existing path (close on merge, orphan recovery, 074 derivation) + // runs as if it had never been forgotten. + adoptedHead := nonEmpty(scan.Marker.Head, obs.HeadSHA) + store.MarkAdopted(key, adoptedHead, dead, now) + rec, _ := store.Get(key) + switch { + case sup.GetMerged(): + c.info("stage labels: adoption marker found with a merged copy; closing the original", logArgs...) + return c.closeSupersededOriginal(ctx, store, key, obs, rec, sup, now) + case strings.EqualFold(sup.GetState(), "closed"): + if AdoptionLoopFromMarkers(scan, now) { + // The marker path has no first-orphan record; the comment + // history is it: a second copy closed within the hour. + store.NoteAdoptionOrphaned(key, dead, now) + store.MarkAdoptionEscalated(key, adoptedHead, stagelabels.LaneReasonAdoptionChildClosedTwice) + c.warn("stage labels: adoption copies closed twice within the hour (from markers); parking for a person", append(logArgs, "reason", stagelabels.LaneReasonAdoptionChildClosedTwice)...) + if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(AdoptionChildClosedTwiceComment(obs, 0, dead))}); err != nil { + return 0, err + } + return 1, nil + } + c.warn("stage labels: adoption marker found with a closed copy; the store had forgotten it", logArgs...) + return c.recoverOrphanedAdoption(ctx, store, key, obs, rec, policy, now) + } + c.info("stage labels: adoption marker found with an open copy; adoption restored on record", logArgs...) + return 0, nil +} diff --git a/src/pkg/github/automerge/automerge_adoption_orphan_test.go b/src/pkg/github/automerge/automerge_adoption_orphan_test.go new file mode 100644 index 0000000000..e0372e74bd --- /dev/null +++ b/src/pkg/github/automerge/automerge_adoption_orphan_test.go @@ -0,0 +1,343 @@ +package automerge + +import ( + "strconv" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Orphaned adoptions (hivecommons/hive#15052): the hive's copy is closed +// without merging while the adoption stands. + +func commentsWithPrefix(api *stageAPI, prefix string) []string { + var out []string + for _, c := range api.comments { + if strings.HasPrefix(c, prefix) { + out = append(out, c) + } + } + return out +} + +// adoptAt074 adopts #7 (head aaa, copy #90) and leaves it labelled 074. +func adoptAt074(t *testing.T) (*adoptHarness, func(), []string) { + t.Helper() + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, adoptPolicy()) + 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 { + t.Fatalf("setup: pulls=%d", len(h.adopt.pulls)) + } + return h, done, []string{"stage/074-adopted-by-hive", "role/contributor", "author/clanker"} +} + +// Copy closed unmerged → the adoption is orphaned and re-run from the same +// head: a fresh copy, a fresh marker naming it, the original never closed, +// and the PR stays at 074. +func TestAdoptionSupersederClosedUnmergedReadopts(t *testing.T) { + h, done, adopted := adoptAt074(t) + defer done() + h.adopt.setSuperseder(90, "closed", false) + h.api.reset() + h.clock.advance(adoptCheckInterval) + res := h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if len(h.adopt.prEdits) != 0 { + t.Fatalf("original closed although the copy was abandoned: %v", h.adopt.prEdits) + } + if len(h.adopt.pulls) != 2 { + t.Fatalf("orphaned adoption not re-run: pulls=%d", len(h.adopt.pulls)) + } + markers := adoptionComments(h.api) + if len(markers) != 1 || !strings.Contains(markers[0], "superseded_by=91") || !strings.Contains(markers[0], "head=aaa") || res.Comments < 1 { + t.Fatalf("fresh adoption marker: %v res=%+v", h.api.comments, res) + } + if n := commentsWithPrefix(h.api, AdoptionRevertedMarker); len(n) != 0 { + t.Fatalf("reverted although re-adoption worked: %v", n) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 91 || rec.AdoptedHead != "aaa" || rec.AdoptOrphanedBy != 90 || rec.AdoptOrphanCount != 1 { + t.Fatalf("record after re-adoption = %+v", rec) + } + // Still 074; the fresh copy is polled, not re-opened. + h.api.reset() + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if h.api.writes() != 0 || len(h.adopt.pulls) != 2 { + t.Fatalf("churn after re-adoption: added=%v removed=%v comments=%v pulls=%d", h.api.added, h.api.removed, h.api.comments, len(h.adopt.pulls)) + } + // The fresh copy merges: the original closes through the usual path. + h.adopt.setSuperseder(91, "closed", true) + h.api.reset() + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if len(h.adopt.prEdits) != 1 || h.adopt.prEdits[0] != "7:closed" { + t.Fatalf("original not closed once the fresh copy merged: %v", h.adopt.prEdits) + } + if c := commentsWithPrefix(h.api, adoptionCloseMarker); len(c) != 1 || !strings.Contains(c[0], "#91") { + t.Fatalf("close note: %v", h.api.comments) + } +} + +// The fresh copy is closed again within the hour: no third copy; the PR +// parks at 085 with reason adoption-child-closed-twice and stays there. +func TestAdoptionSupersederClosedTwiceParksForPerson(t *testing.T) { + h, done, adopted := adoptAt074(t) + defer done() + h.adopt.setSuperseder(90, "closed", false) + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if len(h.adopt.pulls) != 2 { + t.Fatalf("setup: pulls=%d", len(h.adopt.pulls)) + } + h.adopt.setSuperseder(91, "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("a third copy was opened: pulls=%d", len(h.adopt.pulls)) + } + notes := commentsWithPrefix(h.api, AdoptionChildClosedTwiceMarker) + if len(notes) != 1 || !strings.Contains(notes[0], "#90, then #91") || !strings.Contains(notes[0], "needs a person") { + t.Fatalf("loop-guard note: %v", h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 91 || rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionChildClosedTwice || rec.AdoptOrphanCount != 2 { + t.Fatalf("record after loop guard = %+v", rec) + } + // The escalation holds on the next pass: 085 with the reason named. + h.api.reset() + h.clock.advance(time.Minute) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + rec, _ = h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.Stage != stagelabels.NeedsDecision || rec.Reason != stagelabels.LaneReasonAdoptionChildClosedTwice { + t.Fatalf("stage after loop guard = %v reason=%q", rec.Stage, rec.Reason) + } + if added := h.addedLabels(); len(added) != 1 || added[0] != "stage/085-needs-decision" { + t.Fatalf("085 label: added=%v removed=%v", h.api.added, h.api.removed) + } + // Parked: nothing further happens on later passes. + at085 := []string{"stage/085-needs-decision", "role/contributor", "author/clanker"} + h.api.reset() + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", at085...), "check-failing:unit") + if h.api.writes() != 0 || len(h.adopt.pulls) != 2 || len(h.adopt.prEdits) != 0 { + t.Fatalf("parked PR churned: added=%v removed=%v comments=%v pulls=%d", h.api.added, h.api.removed, h.api.comments, len(h.adopt.pulls)) + } +} + +// A copy closed unmerged a safe distance after the first recovery is a new +// orphan, not a loop: re-adopted again. +func TestAdoptionSupersederClosedAgainAfterWindowReadopts(t *testing.T) { + h, done, adopted := adoptAt074(t) + defer done() + h.adopt.setSuperseder(90, "closed", false) + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + h.adopt.setSuperseder(91, "closed", false) + h.api.reset() + h.clock.advance(stagelabels.AdoptionOrphanLoopWindow + time.Minute) + h.observe(adoptPR(7, "aaa", adopted...), "check-failing:unit") + if len(h.adopt.pulls) != 3 || len(commentsWithPrefix(h.api, AdoptionChildClosedTwiceMarker)) != 0 { + t.Fatalf("second orphan outside the window: pulls=%d comments=%v", len(h.adopt.pulls), h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 92 || rec.AdoptOrphanCount != 2 { + t.Fatalf("record = %+v", rec) + } +} + +// The head moved under the orphaned adoption: no copy of a stale commit; +// the adoption is reverted with a note and the PR rejoins its flow. +func TestAdoptionOrphanedHeadMovedReverts(t *testing.T) { + h, done, adopted := adoptAt074(t) + defer done() + h.adopt.setSuperseder(90, "closed", false) + h.api.reset() + h.clock.advance(adoptCheckInterval) + h.observe(adoptPR(7, "bbb", adopted...), "check-failing:unit") + if len(h.adopt.pulls) != 1 { + t.Fatalf("re-adopted a moved head: pulls=%d", len(h.adopt.pulls)) + } + notes := commentsWithPrefix(h.api, AdoptionRevertedMarker) + if len(notes) != 1 || !strings.Contains(notes[0], "why=head-moved") || !strings.Contains(notes[0], "#90, was closed without merging") || !strings.Contains(notes[0], "moved to a newer commit") { + t.Fatalf("revert note: %v", h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 0 || rec.AdoptedHead != "" || rec.AdoptOrphanCount != 0 { + t.Fatalf("adoption not cleared: %+v", rec) + } + h.api.reset() + h.observe(adoptPR(7, "bbb", adopted...), "check-failing:unit") + if added := h.addedLabels(); len(added) != 1 || added[0] != "stage/072-returned-to-contributor" { + t.Fatalf("back to 072: added=%v removed=%v", h.api.added, h.api.removed) + } +} + +// The fresh copy cannot be opened (the branch write fails): reverted with +// the plain reason rather than left pointing at a dead copy. +func TestAdoptionOrphanedReadoptFailureReverts(t *testing.T) { + h, done, adopted := adoptAt074(t) + defer done() + h.adopt.setSuperseder(90, "closed", false) + h.adopt.mu.Lock() + h.adopt.failRefs = true + h.adopt.mu.Unlock() + 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 without a branch: pulls=%d", len(h.adopt.pulls)) + } + notes := commentsWithPrefix(h.api, AdoptionRevertedMarker) + if len(notes) != 1 || !strings.Contains(notes[0], "why=readopt-failed") || !strings.Contains(notes[0], "could not open a fresh copy") { + t.Fatalf("revert note: %v", h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 0 || rec.AdoptedHead != "" { + t.Fatalf("adoption not cleared: %+v", rec) + } +} + +func adoptedMarkerComment(at time.Time, superseding int, head string) map[string]any { + return map[string]any{ + "id": int64(1000 + superseding), + "body": AdoptionMarkerPrefix + " by=hive reason=contributor-offline mode=supersede superseded_by=" + strconv.Itoa(superseding) + " head=" + head + " -->\n@mallory the hive has adopted this PR.", + "created_at": at.Format(time.RFC3339), + "user": map[string]any{"login": "hive[bot]"}, + } +} + +// requiresHumanAt085 is a PR the store has no adoption for, parked at 085 by +// the reviewer verdict — hivecommons/hive#14947 after the store forgot +// #14977. +func requiresHumanAt085(t *testing.T) (*adoptHarness, func(), func() StageLabelResult) { + t.Helper() + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, adoptPolicy()) + requiresHuman := ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true, DecisionQuestion: "close in favour of the superseding PR?"} + h.e.contributorLaneVerdict = func(string, int, string) ContributorLaneVerdict { return requiresHuman } + at085 := []string{"stage/085-needs-decision", "role/contributor", "author/clanker"} + observe := func() StageLabelResult { + h.e.noteStageVerdict("acme", "widget", 7, requiresHuman) + return h.observe(adoptPR(7, "aaa", at085...), stageReasonDecisionRequested) + } + return h, done, observe +} + +// The record forgot the adoption but the PR wears the marker, and the copy +// it names is closed unmerged: re-adopted from the marker in one pass. +func TestAdoptionMarkerWithClosedCopyReadopts(t *testing.T) { + h, done, observe := requiresHumanAt085(t) + defer done() + h.adopt.mu.Lock() + h.adopt.issueComments = map[int][]map[string]any{7: {adoptedMarkerComment(h.clock.at.Add(-3*time.Hour), 89, "aaa")}} + h.adopt.superseders[89] = map[string]any{"number": 89, "state": "closed", "merged": false} + h.adopt.mu.Unlock() + res := observe() + if h.adopt.commentReads != 1 { + t.Fatalf("comment reads = %d", h.adopt.commentReads) + } + if len(h.adopt.pulls) != 1 || len(h.adopt.prEdits) != 0 { + t.Fatalf("marker orphan not re-adopted: pulls=%d edits=%v", len(h.adopt.pulls), h.adopt.prEdits) + } + markers := adoptionComments(h.api) + if len(markers) != 1 || !strings.Contains(markers[0], "superseded_by=90") || res.Comments < 1 { + t.Fatalf("fresh marker: %v", h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 90 || rec.AdoptedHead != "aaa" || rec.AdoptOrphanedBy != 89 || rec.AdoptMarkerScanHead != "aaa" { + t.Fatalf("record = %+v", rec) + } + // Next pass: the adoption is live again, so the PR is the hive's (074) + // and the comments are not read twice at this head. + h.api.reset() + h.clock.advance(time.Minute) + observe() + if added := h.addedLabels(); len(added) != 1 || added[0] != "stage/074-adopted-by-hive" { + t.Fatalf("074 after marker recovery: added=%v removed=%v", h.api.added, h.api.removed) + } + if h.adopt.commentReads != 1 { + t.Fatalf("comments re-read: %d", h.adopt.commentReads) + } +} + +// Marker names a merged copy the store forgot: the original closes through +// the standard "Adopted → merged" path. +func TestAdoptionMarkerWithMergedCopyClosesOriginal(t *testing.T) { + h, done, observe := requiresHumanAt085(t) + defer done() + h.adopt.mu.Lock() + h.adopt.issueComments = map[int][]map[string]any{7: {adoptedMarkerComment(h.clock.at.Add(-3*time.Hour), 89, "aaa")}} + h.adopt.superseders[89] = map[string]any{"number": 89, "state": "closed", "merged": true} + h.adopt.mu.Unlock() + observe() + if len(h.adopt.prEdits) != 1 || h.adopt.prEdits[0] != "7:closed" || len(h.adopt.pulls) != 0 { + t.Fatalf("original not closed: edits=%v pulls=%d", h.adopt.prEdits, len(h.adopt.pulls)) + } + if c := commentsWithPrefix(h.api, adoptionCloseMarker); len(c) != 1 || !strings.Contains(c[0], "#89") { + t.Fatalf("close note: %v", h.api.comments) + } +} + +// Two markers in the last hour, the latest copy closed: the loop guard +// parks from the comment history alone. +func TestAdoptionMarkersClosedTwiceParksFromHistory(t *testing.T) { + h, done, observe := requiresHumanAt085(t) + defer done() + h.adopt.mu.Lock() + h.adopt.issueComments = map[int][]map[string]any{7: { + adoptedMarkerComment(h.clock.at.Add(-50*time.Minute), 88, "aaa"), + adoptedMarkerComment(h.clock.at.Add(-20*time.Minute), 89, "aaa"), + }} + h.adopt.superseders[89] = map[string]any{"number": 89, "state": "closed", "merged": false} + h.adopt.mu.Unlock() + observe() + if len(h.adopt.pulls) != 0 || len(commentsWithPrefix(h.api, AdoptionChildClosedTwiceMarker)) != 1 { + t.Fatalf("loop guard from markers: pulls=%d comments=%v", len(h.adopt.pulls), h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 89 || rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionChildClosedTwice { + t.Fatalf("record = %+v", rec) + } +} + +// A marker a later note settled (revert, close) is not an adoption, and a +// PR with no marker reads its comments once per head. +func TestAdoptionMarkerSettledOrAbsentIsNotRecovered(t *testing.T) { + h, done, observe := requiresHumanAt085(t) + defer done() + h.adopt.mu.Lock() + h.adopt.issueComments = map[int][]map[string]any{7: { + adoptedMarkerComment(h.clock.at.Add(-3*time.Hour), 89, "aaa"), + {"id": int64(2), "body": AdoptionRevertedMarker + " superseded_by=89 head=aaa why=head-moved -->\nundone", "created_at": h.clock.at.Add(-2 * time.Hour).Format(time.RFC3339)}, + }} + h.adopt.superseders[89] = map[string]any{"number": 89, "state": "closed", "merged": false} + h.adopt.mu.Unlock() + observe() + observe() + if len(h.adopt.pulls) != 0 || h.adopt.commentReads != 1 { + t.Fatalf("settled marker recovered: pulls=%d reads=%d", len(h.adopt.pulls), h.adopt.commentReads) + } + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + if rec.AdoptedBy != 0 || rec.AdoptMarkerScanHead != "aaa" { + t.Fatalf("record = %+v", rec) + } +} + +func TestScanAdoptionMarkers(t *testing.T) { + if _, ok := ParseAdoptionMarker(AdoptionMarkerPrefix + " by=hive reason=x mode=in-place head=aaa -->"); ok { + t.Fatal("in-place marker parsed as supersede") + } + m, ok := ParseAdoptionMarker(" " + AdoptionMarkerPrefix + " by=hive reason=contributor-offline mode=supersede superseded_by=14977 head=bc3a6ea3ac36 -->\ntext") + if !ok || m.SupersededBy != 14977 || m.Head != "bc3a6ea3ac36" { + t.Fatalf("marker = %+v ok=%v", m, ok) + } + if _, ok := ParseAdoptionMarker("Superseded by #14977"); ok { + t.Fatal("plain text parsed as a marker") + } +} diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index c01af38c99..018bc46c01 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -980,7 +980,7 @@ func (c *Engine) settleAdoption(ctx context.Context, store *stagelabels.Store, k if rec.AdoptedBy <= 0 { return 0, nil } - return c.settleSuperseder(ctx, store, key, obs, rec, now) + return c.settleSuperseder(ctx, store, key, obs, rec, policy, now) } // settleSuperseder polls the superseding App PR recorded on an adoption (at @@ -990,8 +990,10 @@ func (c *Engine) settleAdoption(ctx context.Context, store *stagelabels.Store, k // derives while the superseder is on record: a `needs-human` label (085) or // a contributor push after the adoption (072) used to stop the poll, and the // original stayed open with "closed automatically once #N merges" on it. -func (c *Engine) settleSuperseder(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, rec stagelabels.Record, now time.Time) (int, error) { - if obs.Merged || !rec.SupersederPollDue(now, adoptCheckInterval) { +// A copy closed unmerged is an orphaned adoption (hivecommons/hive#15052): +// recoverOrphanedAdoption re-adopts, reverts, or parks it. +func (c *Engine) settleSuperseder(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, rec stagelabels.Record, policy StageLabelPolicy, now time.Time) (int, error) { + if obs.Merged || rec.AdoptionChildClosedTwice() || !rec.SupersederPollDue(now, adoptCheckInterval) { return 0, nil } store.MarkAdoptChecked(key, now) @@ -1004,8 +1006,7 @@ func (c *Engine) settleSuperseder(ctx context.Context, store *stagelabels.Store, case sup.GetMerged(): return c.closeSupersededOriginal(ctx, store, key, obs, rec, sup, now) case strings.EqualFold(sup.GetState(), "closed"): - c.warn("stage labels: adoption superseder closed unmerged; returning PR to the contributor", "repo", obs.Display, "pr", obs.Number, "superseded_by", rec.AdoptedBy) - store.ClearAdoption(key) + return c.recoverOrphanedAdoption(ctx, store, key, obs, rec, policy, now) } return 0, nil } @@ -1572,7 +1573,7 @@ func (c *Engine) escapeStalledAdoption(ctx context.Context, store *stagelabels.S if rec.AdoptionEscalated(obs.HeadSHA) { // A workflow-path refusal is a verdict on the head itself: it holds // until the head moves, whatever the re-home or stall reasons say. - if rec.AdoptEscalatedReason == adoptionWorkflowRefusedReason || (rehomeReason == "" && adoptionStalledReason(obs, rec, policy, now) != "") { + if rec.AdoptEscalatedReason == adoptionWorkflowRefusedReason || rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionChildClosedTwice || (rehomeReason == "" && adoptionStalledReason(obs, rec, policy, now) != "") { return adoptionEscape{Escalated: true, Reason: rec.AdoptEscalatedReason}, 0, nil } store.ClearAdoptionEscalation(key) diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index cacaf9dd95..6386ce481e 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -73,6 +73,10 @@ type adoptAPI struct { branchUpdates []string reruns []int64 baseCheckReads int + // issueComments are served by GET issues/n/comments (the adoption + // marker scan, hivecommons/hive#15052); commentReads counts the calls. + issueComments map[int][]map[string]any + commentReads int } type adoptCommit struct { @@ -167,6 +171,16 @@ func (a *adoptAPI) handle(w http.ResponseWriter, r *http.Request) bool { a.refUpdates = append(a.refUpdates, ref+"="+body.SHA) _ = json.NewEncoder(w).Encode(map[string]any{"ref": ref, "object": map[string]string{"sha": body.SHA}}) return true + case r.Method == http.MethodGet && strings.HasPrefix(path, "/repos/acme/widget/issues/") && strings.HasSuffix(path, "/comments"): + var n int + _, _ = sscanInt(strings.TrimPrefix(path, "/repos/acme/widget/issues/"), &n) + a.commentReads++ + out := a.issueComments[n] + if out == nil { + out = []map[string]any{} + } + _ = json.NewEncoder(w).Encode(out) + return true case r.Method == http.MethodPost && path == "/repos/acme/widget/pulls": var body map[string]any _ = json.NewDecoder(r.Body).Decode(&body) @@ -697,30 +711,6 @@ func TestAdoptionOfflineGraceThenSupersederMergedClosesOriginal(t *testing.T) { } } -// Superseder closed unmerged: the adoption is forgotten and the PR falls -// back to 072 (a maintainer declined the takeover). -func TestAdoptionSupersederClosedUnmergedReturnsTo072(t *testing.T) { - h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, adoptPolicy()) - defer done() - 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") - h.adopt.setSuperseder(90, "closed", false) - h.api.reset() - h.clock.advance(adoptCheckInterval) - h.observe(adoptPR(7, "aaa", "stage/074-adopted-by-hive", "role/contributor", "author/clanker"), "check-failing:unit") - if len(h.adopt.prEdits) != 0 { - t.Fatalf("original closed although superseder was abandoned: %v", h.adopt.prEdits) - } - h.api.reset() - h.observe(adoptPR(7, "aaa", "stage/074-adopted-by-hive", "role/contributor", "author/clanker"), "check-failing:unit") - if added := h.addedLabels(); len(added) != 1 || added[0] != "stage/072-returned-to-contributor" { - t.Fatalf("fallback to 072: added=%v removed=%v", h.api.added, h.api.removed) - } -} - // A new head from the author before the grace re-arms the nudge and is never // adopted; a green head leaves 072 entirely. func TestAdoptionNewHeadFromAuthorNotAdopted(t *testing.T) { diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index b0b66b899b..7a359b6c42 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -1216,7 +1216,15 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) // hivecommons/hive#14471: the superseder is still on record while // the lane derives 085 (needs-human) or 072 (a later contributor // head). Its merge must still close this PR. - n, err := c.settleSuperseder(ctx, store, key, obs, rec, now) + n, err := c.settleSuperseder(ctx, store, key, obs, rec, policy, now) + result.Comments += n + if err != nil && firstErr == nil { + firstErr = err + } + } else if ok && adoptionMarkerScanDue(obs, rec, ok, want.Stage, policy) { + // hivecommons/hive#15052: the record forgot its adoption but the + // PR may still wear the marker; settle it from the copy's state. + n, err := c.recoverOrphanedAdoptionFromMarker(ctx, store, key, obs, policy, now) result.Comments += n if err != nil && firstErr == nil { firstErr = err diff --git a/src/pkg/stagelabels/adoption_orphan_test.go b/src/pkg/stagelabels/adoption_orphan_test.go new file mode 100644 index 0000000000..e659cc8728 --- /dev/null +++ b/src/pkg/stagelabels/adoption_orphan_test.go @@ -0,0 +1,78 @@ +package stagelabels + +import ( + "path/filepath" + "testing" + "time" +) + +// Orphaned-adoption memory (hivecommons/hive#15052): the closed copy, the +// loop window, the loop-guard park, the once-per-head marker scan; all of +// it resets with the adoption. +func TestStoreAdoptionOrphaned(t *testing.T) { + now := time.Date(2026, 10, 28, 15, 46, 0, 0, time.UTC) + st := NewStore(filepath.Join(t.TempDir(), "s.json"), func() time.Time { return now }) + key := Key("hivecommons/hive", 14947) + + // Unknown key or no copy: no-op. + st.NoteAdoptionOrphaned(key, 14977, now) + st.MarkAdoptMarkerScanned(key, "bc3a6ea") + if _, ok := st.Get(key); ok { + t.Fatal("note created a record") + } + st.Observe(key, AdoptedByHive, "") + st.MarkAdopted(key, "bc3a6ea", 14977, now.Add(-6*time.Minute)) + st.NoteAdoptionOrphaned(key, 0, now) + if r, _ := st.Get(key); r.AdoptOrphanCount != 0 || r.AdoptionOrphanedWithin(now, AdoptionOrphanLoopWindow) { + t.Fatalf("empty note recorded: %+v", r) + } + + st.NoteAdoptionOrphaned(key, 14977, now) + r, _ := st.Get(key) + if r.AdoptOrphanedBy != 14977 || !r.AdoptOrphanedAt.Equal(now) || r.AdoptOrphanCount != 1 { + t.Fatalf("orphan not recorded: %+v", r) + } + if !r.AdoptionOrphanedWithin(now.Add(59*time.Minute), AdoptionOrphanLoopWindow) || r.AdoptionOrphanedWithin(now.Add(AdoptionOrphanLoopWindow), AdoptionOrphanLoopWindow) { + t.Fatal("loop window misread") + } + // The fresh copy keeps the orphan memory; a second orphan counts up. + st.MarkAdopted(key, "bc3a6ea", 15060, now) + st.NoteAdoptionOrphaned(key, 15060, now.Add(10*time.Minute)) + r, _ = st.Get(key) + if r.AdoptedBy != 15060 || r.AdoptOrphanedBy != 15060 || r.AdoptOrphanCount != 2 { + t.Fatalf("second orphan: %+v", r) + } + + // Loop-guard park: escalated at the adopted head for the reason. + if r.AdoptionChildClosedTwice() { + t.Fatal("parked before escalation") + } + st.MarkAdoptionEscalated(key, "bc3a6ea", LaneReasonAdoptionChildClosedTwice) + r, _ = st.Get(key) + if !r.AdoptionChildClosedTwice() { + t.Fatalf("park not read: %+v", r) + } + st.MarkAdoptionEscalated(key, "bc3a6ea", "adoption-stalled:x") + if r, _ = st.Get(key); r.AdoptionChildClosedTwice() { + t.Fatal("another reason read as the park") + } + + // Marker scan: once per head, idempotent, empty head ignored. + st.MarkAdoptMarkerScanned(key, "") + st.MarkAdoptMarkerScanned(key, "bc3a6ea") + st.MarkAdoptMarkerScanned(key, "bc3a6ea") + if r, _ = st.Get(key); r.AdoptMarkerScanHead != "bc3a6ea" { + t.Fatalf("scan head: %+v", r) + } + + // The adoption's end forgets the orphan memory; the scan head survives + // (the comments do not change when the store does). + st.ClearAdoption(key) + r, _ = st.Get(key) + if r.AdoptOrphanedBy != 0 || !r.AdoptOrphanedAt.IsZero() || r.AdoptOrphanCount != 0 || r.AdoptMarkerScanHead != "bc3a6ea" { + t.Fatalf("clear: %+v", r) + } + if r.AdoptionOrphanedWithin(now, AdoptionOrphanLoopWindow) || r.AdoptionChildClosedTwice() { + t.Fatal("cleared record still reads orphaned") + } +} diff --git a/src/pkg/stagelabels/store.go b/src/pkg/stagelabels/store.go index 412ec1be16..2aad363c8d 100644 --- a/src/pkg/stagelabels/store.go +++ b/src/pkg/stagelabels/store.go @@ -201,6 +201,21 @@ type Record struct { // update-branch commits forever; both reset with the adoption. AdoptRecoveryAttempt string `json:"adopt_recovery_attempt,omitempty"` AdoptRecoveryCount int `json:"adopt_recovery_count,omitempty"` + // AdoptOrphanedBy / AdoptOrphanedAt / AdoptOrphanCount record that a + // supersede-mode adoption's hive copy was closed without merging + // (hivecommons/hive#15052: the duplicate-fix sweep closed #14977 under + // #14947). The reconciler re-adopts from the same head; the count and + // time let it stop when the fresh copy is closed again within + // AdoptionOrphanLoopWindow (reason LaneReasonAdoptionChildClosedTwice). + // All three reset with the adoption. + AdoptOrphanedBy int `json:"adopt_orphaned_by,omitempty"` + AdoptOrphanedAt time.Time `json:"adopt_orphaned_at,omitempty"` + AdoptOrphanCount int `json:"adopt_orphan_count,omitempty"` + // AdoptMarkerScanHead is the head at which the reconciler last read the + // PR's comments for an adoption marker the store no longer carries (a + // record that forgot its adoption, or a store rebuilt from scratch); + // one read per head. + AdoptMarkerScanHead string `json:"adopt_marker_scan_head,omitempty"` // MergeCommitNotedHead is the head the `merge-method/merge` note was // posted for, so the lane explains the merge method once per head. MergeCommitNotedHead string `json:"merge_commit_noted_head,omitempty"` @@ -1104,6 +1119,57 @@ func (s *Store) NoteAdoptionBlocked(key, head, reason string, now time.Time) { s.dirty = true } +// AdoptionOrphanLoopWindow is how soon after one orphan recovery a second +// closed copy counts as a loop: the PR then parks at 085 with +// LaneReasonAdoptionChildClosedTwice instead of a third copy being opened. +const AdoptionOrphanLoopWindow = time.Hour + +// LaneReasonAdoptionChildClosedTwice is the 085 reason for an adopted PR +// whose hive copy was closed unmerged twice within AdoptionOrphanLoopWindow: +// something keeps closing the copy and a person must say what. +const LaneReasonAdoptionChildClosedTwice = "adoption-child-closed-twice" + +// NoteAdoptionOrphaned records that superseding PR `by` was closed without +// merging while the adoption stood (hivecommons/hive#15052). +func (s *Store) NoteAdoptionOrphaned(key string, by int, now time.Time) { + s.mu.Lock() + defer s.mu.Unlock() + r, ok := s.items[key] + if !ok || by <= 0 { + return + } + r.AdoptOrphanedBy, r.AdoptOrphanedAt = by, now + r.AdoptOrphanCount++ + s.items[key] = r + s.dirty = true +} + +// AdoptionOrphanedWithin reports whether an orphan was recorded less than +// window before now: a second closed copy inside it is a loop. +func (r Record) AdoptionOrphanedWithin(now time.Time, window time.Duration) bool { + return r.AdoptOrphanCount > 0 && !r.AdoptOrphanedAt.IsZero() && now.Sub(r.AdoptOrphanedAt) < window +} + +// AdoptionChildClosedTwice reports the loop-guard park: the adoption stands +// on record, escalated at its head for LaneReasonAdoptionChildClosedTwice. +func (r Record) AdoptionChildClosedTwice() bool { + return r.AdoptEscalatedReason == LaneReasonAdoptionChildClosedTwice && r.AdoptEscalatedHead != "" && r.AdoptEscalatedHead == r.AdoptedHead +} + +// MarkAdoptMarkerScanned records that the PR's comments were read for an +// adoption marker at head. +func (s *Store) MarkAdoptMarkerScanned(key, head string) { + s.mu.Lock() + defer s.mu.Unlock() + r, ok := s.items[key] + if !ok || head == "" || r.AdoptMarkerScanHead == head { + return + } + r.AdoptMarkerScanHead = head + s.items[key] = r + s.dirty = true +} + // ClearAdoptionBlocked forgets a held adoption (it went ahead, or the // policy no longer holds it). func (s *Store) ClearAdoptionBlocked(key string) { @@ -1243,6 +1309,7 @@ func (s *Store) ClearAdoption(key string) { r.AdoptMode, r.AdoptReason, r.AdoptDiffFence = "", "", "" r.AdoptedHeadAt, r.AdoptEscalatedHead, r.AdoptEscalatedReason, r.AdoptEscalationNotedHead = time.Time{}, "", "", "" r.AdoptRecoveryAttempt, r.AdoptRecoveryCount = "", 0 + r.AdoptOrphanedBy, r.AdoptOrphanedAt, r.AdoptOrphanCount = 0, time.Time{}, 0 r.SupersededBy, r.SupersededAt, r.SupersededOpen, r.SupersederMergedNotedHead = 0, time.Time{}, false, "" s.items[key] = r s.dirty = true