From dd60819263213480661855900433e47f64ef5889 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 28 Oct 2026 18:47:51 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Adoption:=20close=20the=20original?= =?UTF-8?q?=20first=20when=20superseding,=20reopen=20if=20the=20hive's=20c?= =?UTF-8?q?opy=20fails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Supersede adoption now closes the contributor's original in the same pass that opens its hive-authored copy (push branch → open copy → one note naming the copy and why → hive/superseded → close), so one issue never shows two active PRs. The copy is polled from the stage store; closed unmerged or red required checks at two heads reopen the original with one note, remove the label and park it at 085 as adoption-copy-failed (one reopen per 24h). governor.labels.stage.adoption.supersede.close_first (default true) keeps the old keep-open behaviour when false. Fixes #15135 Signed-off-by: Andrew Anderson --- changelog.d/changed-adoption-close-first.md | 1 + src/cmd/hive/merge_eligibility.go | 1 + src/docs/dashboard-sections.md | 4 +- src/docs/labels-and-control-signals.md | 49 ++- src/docs/operator-reference.md | 13 +- src/hive.yaml.example | 4 + src/pkg/config/adoption_close_first_test.go | 29 ++ src/pkg/config/config.go | 20 + .../config/testdata/agent_runtime_golden.json | 5 +- src/pkg/dashboard/stage_tracker.go | 16 +- ...stage_tracker_adoption_close_first_test.go | 62 +++ src/pkg/dashboard/stage_tracker_decision.go | 2 + src/pkg/dashboard/static/index.html | 5 +- .../automerge_adoption_close_first.go | 285 ++++++++++++++ .../automerge_adoption_close_first_test.go | 366 ++++++++++++++++++ .../automerge/automerge_adoption_orphan.go | 2 +- .../automerge/automerge_contributor_adopt.go | 22 +- .../automerge/automerge_stage_labels.go | 14 + .../stagelabels/adoption_close_first_test.go | 119 ++++++ src/pkg/stagelabels/store.go | 130 ++++++- 20 files changed, 1128 insertions(+), 21 deletions(-) create mode 100644 changelog.d/changed-adoption-close-first.md create mode 100644 src/pkg/config/adoption_close_first_test.go create mode 100644 src/pkg/dashboard/stage_tracker_adoption_close_first_test.go create mode 100644 src/pkg/github/automerge/automerge_adoption_close_first.go create mode 100644 src/pkg/github/automerge/automerge_adoption_close_first_test.go create mode 100644 src/pkg/stagelabels/adoption_close_first_test.go diff --git a/changelog.d/changed-adoption-close-first.md b/changelog.d/changed-adoption-close-first.md new file mode 100644 index 0000000000..d6b58770c2 --- /dev/null +++ b/changelog.d/changed-adoption-close-first.md @@ -0,0 +1 @@ +- Supersede adoption is close-first: the hive closes the contributor's original in the same pass that opens its copy (one note naming the copy and why, `hive/superseded`), so an issue never shows two active PRs; a copy that is closed unmerged or red at two heads reopens the original (`adoption-copy-failed`, at most once per 24h). `governor.labels.stage.adoption.supersede.close_first: false` keeps the original open until the copy merges (#15135). diff --git a/src/cmd/hive/merge_eligibility.go b/src/cmd/hive/merge_eligibility.go index 72117450ca..2ecf895db8 100644 --- a/src/cmd/hive/merge_eligibility.go +++ b/src/cmd/hive/merge_eligibility.go @@ -119,6 +119,7 @@ func stageLabelPolicyFunc(cfg *config.Config) func() automerge.StageLabelPolicy AdoptionStrictAttribution: stage.Adoption.StrictAttributionEnabled(), AdoptionAutoMerge: stage.Adoption.AutoMergeEnabled(), AdoptionMaintainerPolicy: stage.Adoption.EffectiveMaintainerPolicy(), + AdoptSupersedeCloseFirst: stage.Adoption.Supersede.CloseFirstEnabled(), HiveID: cfg.HiveID, AuthorKindLabels: cfg.Governor.Labels.Author.Names(), RelayKnown: contributorRelayKnown, diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index c40c1865bd..5c6a34c041 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -682,7 +682,7 @@ 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. +**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. With close-first supersession (the default, hivecommons/hive#15135) the original is closed in the adopting pass and has **no row** while its copy is open — the copy is the one active PR for the issue, and the original's record is superseded (no chevron, no *Needs human decision*). A copy that then fails (closed unmerged, or red at two heads) reopens the original: `adopted_by_state: "copy-failed"` renders "Adopted → #N (copy failed) · reopened, needs a person" at 085 with the caption "The hive's copy #N failed; this PR was reopened and needs a person". **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. @@ -769,7 +769,7 @@ 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. +**Orphaned adoption.** (Keep-open supersession, `close_first: false`; a close-first original whose copy closed is reopened by the lane instead, see above.) 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. diff --git a/src/docs/labels-and-control-signals.md b/src/docs/labels-and-control-signals.md index eca3ebbeb7..90ab2968d0 100644 --- a/src/docs/labels-and-control-signals.md +++ b/src/docs/labels-and-control-signals.md @@ -303,7 +303,8 @@ stateDiagram-v2 s074 --> s080: in-place fix green and approved (adoption outranks 050/060; the merge gate takes it) s074 --> s095: lane admits the hive's head, soak running s074 --> s100: lane admits the hive's head, soak complete - s074 --> [*]: superseder merged (original closed) + s074 --> [*]: superseder merged (original closed); close_first (default) closes the original in the adopting pass instead + s074 --> s085: close-first copy closed unmerged or red at two heads → original reopened, adoption-copy-failed s074 --> s072: superseder closed unmerged, or contributor pushed over an in-place adoption (handed back; 050 if that head is green) s074 --> s085: adopted head unchanged past the dwell with CI idle and a real blocker the hive cannot push past (adoption-stalled:; a red optional check alone never counts), or the re-home failed s085 --> s074: the adoption-stalled reason cleared (CI re-ran, check green, verdict landed); escalation released @@ -1097,10 +1098,45 @@ When a clanker-authored contributor PR goes red the hive does not just relabel i carries the original's `Closes/Fixes/Resolves` lines and instructs the fix agent to rebase on the base and fix CI **on that branch** without opening another PR; - - posts ONE `` comment on the original and moves it to - `stage/074-adopted-by-hive` (its linked issue mirrors the stage). The - hub drops the PR from the contributor's fix queue (either mode). + - **close-first** (`governor.labels.stage.adoption.supersede.close_first`, + default on; hivecommons/hive#15135 — the operator saw two active PRs + for one issue, #15029 and its copy #15123, and had not asked for + either): in the same pass, right after the copy opens, posts ONE + `` comment reading + "Superseded by hive: re-authored as #M with the same commits and + sign-off because . This PR is + closed; if #M fails, hive reopens it.", applies `hive/superseded`, and + **closes the original**. The order is push branch → open copy → note → + label → close, never with a sweep between, so the note names the copy + and the Activity Monitor never shows the original as active once the + copy exists: the stage record is superseded (no row, no chevron, no + needs-human) and the copy is the one PR for the issue. A close GitHub + refuses (403/404) leaves the original open with the note, superseded + all the same. `close_first: false` restores the keep-open behaviour + below: the note has no `close_first` field, the original moves to + `stage/074-adopted-by-hive` (its linked issue mirrors the stage) and + closes only when the copy merges. The hub drops the PR from the + contributor's fix queue in every mode. + - **Reopen on failure** (close-first only). The hive polls the copy from + the stage store at most every 10 minutes (a closed original is in no + lane's listing). Copy merged → done. Copy closed unmerged, or its + required checks red at **two** distinct copy heads (the fixer's one + repair push did not take; the same loop cap as the rebase/adoption + guard) → the original is **reopened**, gets ONE + `` + note ("Hive's copy #M failed (); reopening this PR; needs a + person"), loses `hive/superseded`, and parks at `stage/085` with reason + `adoption-copy-failed` (tracker word "Adopted → #M (copy failed) · + reopened, needs a person"; the park holds until a person acts, an + `adoption/supersede` request re-runs the takeover from the same head). + **Ping-pong guard:** one reopen per original per 24h — a second failed + copy inside the window leaves the original closed with a `held=true` + note saying so. The orphan recovery below never runs for a close-first + original (its copy being closed is a failure, not an orphan); the + `hive:adoption-copy-failed` note is a settled marker, so the marker scan + never re-adopts a reopened original on its own. The superseding PR is App-authored and red with the same failures, so the existing machinery takes over: it lands in `ci-failing.json` and the scanner's FIX-BEFORE-NEW kick (the route every red hive-authored PR takes, @@ -1111,7 +1147,8 @@ When a clanker-authored contributor PR goes red the hive does not just relabel i superseded PR is never re-homed again. One adoption attempt per contributor head, whatever the outcome — the DCO re-home likewise records 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 + not retried on the next sweep. While in 074 (keep-open mode, + `close_first: false`) the hive polls the superseder at most every 10 minutes: merged → one `` note and the original is closed; closed unmerged → the adoption is **orphaned** and the hive recovers it rather than leaving the original parked diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index 8c7dd318c5..8449f7b800 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -703,8 +703,17 @@ not consulted, because the hive owns an adopted head (`governor.labels.stage.adoption.auto_merge`, default on; `false` leaves an untrusted contributor's adopted PR waiting for a maintainer `/lgtm`). If the hive cannot push in place, it pushes `hive/adopt-` at the contributor head, opens a superseding App PR -that preserves the author's commits, moves the original to 074 and closes it -once the superseder merges. Whether that rework reaches each agent is visible per row +that preserves the author's commits, and — in the same pass — **closes the +original** with one note naming the copy and why ("Superseded by hive: +re-authored as #M … This PR is closed; if #M fails, hive reopens it."), +labelled `hive/superseded`, so one issue never shows two active PRs +(`governor.labels.stage.adoption.supersede.close_first`, default on; +hivecommons/hive#15135). If the copy is closed unmerged or its required +checks are red at two heads, the hive reopens the original, removes +`hive/superseded`, and parks it at `stage/085` with `adoption-copy-failed` +("needs a person"); at most one reopen per original per 24h. `close_first: +false` keeps the earlier behaviour: the original moves to 074 and closes +only once the superseder merges. Whether that rework reaches each agent is visible per row on Operations → Connected contributor agents: the relay's parallel capacity (`capacity N · spawn|preempt`, or *not advertised*), one line per in-flight item tagged **NEW** / **REWORK** with its stage label, and a red *N PRs diff --git a/src/hive.yaml.example b/src/hive.yaml.example index 295c6dbcd4..c373cad9af 100644 --- a/src/hive.yaml.example +++ b/src/hive.yaml.example @@ -525,6 +525,10 @@ governor: # after_offline: 15m # default 15m; registered relay offline, measured from the later # # of its last heartbeat and the first stage/072 nudge # after_unregistered: 15m # default 15m; no contributor profile, wait after the nudge with no new head + # supersede: + # close_first: true # default true; close the original (hive/superseded, one note naming the + # # copy) in the pass that opens the copy; reopened if the copy fails. + # # false: original stays stage/074 and closes when the copy merges # Optional: read actionable work from Linear instead of GitHub Issues. # Secrets should come from env interpolation, not literal values. diff --git a/src/pkg/config/adoption_close_first_test.go b/src/pkg/config/adoption_close_first_test.go new file mode 100644 index 0000000000..89fb4802dc --- /dev/null +++ b/src/pkg/config/adoption_close_first_test.go @@ -0,0 +1,29 @@ +package config + +import ( + "strings" + "testing" + + "gopkg.in/yaml.v3" +) + +// governor.labels.stage.adoption.supersede.close_first (hivecommons/hive#15135): +// on by default; false keeps the original open until the copy merges. +func TestStageAdoptionSupersedeCloseFirst(t *testing.T) { + if !(StageAdoptionConfig{}).Supersede.CloseFirstEnabled() { + t.Fatal("close_first default is not true") + } + for in, want := range map[string]bool{ + "supersede: {}\n": true, + "supersede:\n close_first: true\n": true, + "supersede:\n close_first: false\n": false, + } { + var cfg StageAdoptionConfig + if err := yaml.NewDecoder(strings.NewReader(in)).Decode(&cfg); err != nil { + t.Fatalf("%q: %v", in, err) + } + if got := cfg.Supersede.CloseFirstEnabled(); got != want { + t.Errorf("%q: CloseFirstEnabled = %v, want %v", in, got, want) + } + } +} diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index f527ee6a70..dd1caa7150 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -2836,6 +2836,26 @@ type StageAdoptionConfig struct { // AdoptionMaintainerPolicyNever is the pre-#14524 rule: the hive never // takes a maintainer's PR over and releases any adoption of one. MaintainerPolicy string `yaml:"maintainer_policy,omitempty"` + // Supersede tunes supersede-mode adoption (the hive-authored copy from + // hive/adopt-). + Supersede StageAdoptionSupersedeConfig `yaml:"supersede,omitempty"` +} + +// StageAdoptionSupersedeConfig is governor.labels.stage.adoption.supersede. +type StageAdoptionSupersedeConfig struct { + // CloseFirst closes the original PR as soon as the hive's copy is open + // (hivecommons/hive#15135): one `hive/superseded` note names the copy, + // and the hive reopens the original if the copy is closed unmerged, its + // required checks stay red across two heads, or the adoption branch + // cannot be pushed. nil = true. False keeps the pre-#15135 behaviour: + // the original stays open at 074 until the copy merges. + CloseFirst *bool `yaml:"close_first,omitempty"` +} + +// CloseFirstEnabled reports whether a supersede adoption closes the +// original as soon as the copy exists (default on). +func (c StageAdoptionSupersedeConfig) CloseFirstEnabled() bool { + return c.CloseFirst == nil || *c.CloseFirst } // Values for governor.labels.stage.adoption.maintainer_policy. diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index e04a7a1ec2..5fc22bcdca 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -202,7 +202,10 @@ "InPlace": null, "StrictAttribution": null, "AutoMerge": null, - "MaintainerPolicy": "" + "MaintainerPolicy": "", + "Supersede": { + "CloseFirst": null + } }, "Stalled": { "RequireQuiet": null diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 4ce934baf9..67f6611c6b 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -405,7 +405,9 @@ type StageTrackerItem struct { // 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. + // closed within the hour, so the hive stopped and a person must decide; + // "copy-failed" — a close-first copy failed and the original was + // reopened for a person (hivecommons/hive#15135). 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 @@ -1162,6 +1164,8 @@ func humanizeStageReason(reason string) string { switch lower { case stagelabels.LaneReasonAdoptionChildClosedTwice: return "the hive's copies of this PR were closed without merging twice within an hour" + case stagelabels.LaneReasonAdoptionCopyFailed: + return "the hive's copy of this PR failed and this PR was reopened" case "held": return "held by a hold label" case "label:needs-rebase": @@ -1490,8 +1494,11 @@ func stageTrackerCaption(stage stagelabels.Stage, reason, author string, kind st 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 { + switch stageTrackerAdoptedByState(rec) { + case stageTrackerAdoptedClosedTwice: return stageTrackerAdoptionLoopCaption(rec), "maintainer" + case stageTrackerAdoptedCopyFailed: + return "The hive's copy #" + strconv.Itoa(rec.AdoptedBy) + " failed; this PR was reopened and needs a person", "maintainer" } if step, _ := stageTrackerUnauthorized(reason); step != "" { // hivecommons/hive#14589: not a decision but a step only a @@ -1582,6 +1589,9 @@ func timePtr(t time.Time) *time.Time { const ( stageTrackerAdoptedClosed = "closed" stageTrackerAdoptedClosedTwice = "closed-twice" + // stageTrackerAdoptedCopyFailed: a close-first supersession whose copy + // failed; the original was reopened for a person (hivecommons/hive#15135). + stageTrackerAdoptedCopyFailed = "copy-failed" ) // stageTrackerAdoptedByState reads the copy's state from the record @@ -1593,6 +1603,8 @@ func stageTrackerAdoptedByState(rec stagelabels.Record) string { return "" case rec.AdoptionChildClosedTwice(): return stageTrackerAdoptedClosedTwice + case rec.AdoptionCopyFailed(): + return stageTrackerAdoptedCopyFailed case rec.AdoptOrphanedBy == rec.AdoptedBy: return stageTrackerAdoptedClosed } diff --git a/src/pkg/dashboard/stage_tracker_adoption_close_first_test.go b/src/pkg/dashboard/stage_tracker_adoption_close_first_test.go new file mode 100644 index 0000000000..ec2fec8cad --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_adoption_close_first_test.go @@ -0,0 +1,62 @@ +package dashboard + +import ( + "encoding/json" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Close-first supersession (hivecommons/hive#15135): the closed original +// never reaches the tracker; a reopened one reads "copy failed", parked for +// a person, and is not "recovering". + +func TestStageTrackerCloseFirstOriginalNotRendered(t *testing.T) { + now := time.Date(2026, 10, 29, 9, 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", 15123, now) + store.MarkSupersededClosedFirst(key, 15123, false, now) + // Even if the status still lists the original (the close has not + // propagated), the superseded record drops the row. + for _, it := range s.buildStageTracker(status, store, now).Items { + if it.Kind == "pr" && it.Number == 14947 { + t.Fatalf("superseded original rendered: %+v", it) + } + } +} + +func TestStageTrackerCloseFirstCopyFailedSaysWhy(t *testing.T) { + now := time.Date(2026, 10, 29, 9, 0, 0, 0, time.UTC) + s, store, status, key := orphanTrackerFixture(t, now, stagelabels.NeedsDecision, stagelabels.LaneReasonAdoptionCopyFailed, []string{"stage/085-needs-decision"}) + store.MarkAdopted(key, "bc3a6ea", 15123, now.Add(-time.Hour)) + store.MarkSupersededClosedFirst(key, 15123, false, now.Add(-time.Hour)) + store.MarkAdoptionReopened(key, 15123, now) + store.MarkAdoptionEscalated(key, "bc3a6ea", stagelabels.LaneReasonAdoptionCopyFailed) + + it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 14947) + if it.AdoptedBy != 15123 || it.AdoptedByState != "copy-failed" { + t.Fatalf("adopted_by=%d state=%q", it.AdoptedBy, it.AdoptedByState) + } + if !strings.Contains(it.Caption, "copy #15123 failed") || !strings.Contains(it.Caption, "reopened") || !strings.Contains(it.Caption, "needs a person") || strings.Contains(it.Caption, "fresh copy") { + t.Fatalf("caption = %q", it.Caption) + } + if it.DecisionNeeded == nil || !strings.Contains(it.DecisionNeeded.Question, "#15123") || !strings.Contains(it.DecisionNeeded.Question, "reopened") { + t.Fatalf("decision_needed = %+v", it.DecisionNeeded) + } + if got := humanizeStageReason(stagelabels.LaneReasonAdoptionCopyFailed); !strings.Contains(got, "copy of this PR failed") { + t.Fatalf("humanizeStageReason = %q", got) + } + raw, _ := json.Marshal(it) + if !strings.Contains(string(raw), `"adopted_by_state":"copy-failed"`) { + t.Fatalf("payload lacks adopted_by_state: %s", raw) + } + // An operator re-adopts: the fresh copy clears the state. + store.MarkAdopted(key, "bc3a6ea", 15200, now.Add(time.Minute)) + store.Observe(key, stagelabels.AdoptedByHive, "contributor-lane:adopted") + it = trackerItem(t, s.buildStageTracker(status, store, now.Add(time.Minute)), "pr", 14947) + if it.AdoptedBy != 15200 || it.AdoptedByState != "" { + t.Fatalf("after re-adopt adopted_by=%d state=%q", it.AdoptedBy, it.AdoptedByState) + } +} diff --git a/src/pkg/dashboard/stage_tracker_decision.go b/src/pkg/dashboard/stage_tracker_decision.go index 7157c37fcb..07f47e061b 100644 --- a/src/pkg/dashboard/stage_tracker_decision.go +++ b/src/pkg/dashboard/stage_tracker_decision.go @@ -519,6 +519,8 @@ func decisionForPR(it StageTrackerItem, verdict *review.Aggregate, sentinelLabel 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 lower == stagelabels.LaneReasonAdoptionCopyFailed: + d.Question = "The hive's copy of this PR (#" + strconv.Itoa(it.AdoptedBy) + ") failed, so this PR was reopened. Ask the hive to supersede it again, 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 377bad9944..a3d4629bcb 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -13968,6 +13968,7 @@

Where to get help

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'; + if (state === 'copy-failed') return 'Adopted → #' + by + ' (copy failed) · reopened, needs a person'; return 'Adopted → #' + by; } const STAGE_TRACKER_UNAUTHORIZED_WORD = 'Awaiting human'; @@ -42943,7 +42944,7 @@

Configure LiteLLM

{ tab: 'Labels', section: 'Labels', label: 'Stage dwell overrides', key: 'governor.labels.stage.dwell', text: 'stage dwell stalled threshold governor.labels.stage.dwell' }, { tab: 'Labels', section: 'Labels', label: 'Adoption of a maintainer\'s PR', key: 'governor.labels.stage.adoption.maintainer_policy', text: 'adoption maintainer policy adopt ask never maintainer own pr clanker disconnected offline adopt action governor.labels.stage.adoption.maintainer_policy' }, { tab: 'Labels', section: 'Labels', label: 'Contributor nudge', key: 'governor.labels.stage.contributor_nudge.enabled', text: 'contributor nudge returned-to-contributor red ci fix-pr relay interrupt renudge presence window governor.labels.stage.contributor_nudge.enabled governor.labels.stage.contributor_nudge.renudge_interval governor.labels.stage.presence_window' }, - { tab: 'Labels', section: 'Labels', label: 'Adopt red contributor PRs', key: 'governor.labels.stage.adoption.enabled', text: 'adoption adopted-by-hive stage 074 red contributor pr grace online offline supersede takeover in place after offline unregistered heartbeat governor.labels.stage.adoption.enabled governor.labels.stage.contributor_grace_online governor.labels.stage.contributor_grace_offline governor.labels.stage.adoption.after_offline governor.labels.stage.adoption.after_unregistered governor.labels.stage.adoption.in_place governor.labels.stage.adoption.strict_attribution strict attribution governor.labels.stage.adoption.auto_merge auto merge adopted head hive authority' }, + { tab: 'Labels', section: 'Labels', label: 'Adopt red contributor PRs', key: 'governor.labels.stage.adoption.enabled', text: 'adoption adopted-by-hive stage 074 red contributor pr grace online offline supersede takeover in place after offline unregistered heartbeat governor.labels.stage.adoption.enabled governor.labels.stage.contributor_grace_online governor.labels.stage.contributor_grace_offline governor.labels.stage.adoption.after_offline governor.labels.stage.adoption.after_unregistered governor.labels.stage.adoption.in_place governor.labels.stage.adoption.strict_attribution strict attribution governor.labels.stage.adoption.auto_merge auto merge adopted head hive authority governor.labels.stage.adoption.supersede.close_first close first superseded reopen copy failed' }, { tab: 'Hub', section: 'Contributor Relay', label: 'Fix budget', key: 'contribute.max_parallel_fixes', text: 'fix budget parallel fixes focus window red pr fix-pr preempt spawn contribute.max_parallel_fixes contribute.min_focus_window' }, { tab: 'Logging', section: 'Logging', label: 'Governor logging', key: 'governor.logging.max_size_mb', text: 'governor logging directory level compression max size age backups governor.logging.compress governor.logging.dir governor.logging.level governor.logging.max_age_days governor.logging.max_backups governor.logging.max_size_mb' }, { tab: 'Advisory', section: 'Advisory', label: 'Advisory digest', key: 'governor.advisory.max_findings', text: 'advisory digest max findings show all staleness pr autoclose target linear issue retry backoff' }, @@ -49820,7 +49821,7 @@

Stage labels

-
Go duration after the comment-only nudge before an offline or external author's red PR is adopted. With live hub presence the hive instead waits ${yamlHint('governor.labels.stage.adoption.after_offline')} (default 30m) after a registered relay's last heartbeat, or ${yamlHint('governor.labels.stage.adoption.after_unregistered')} (default 2h) for an author with no profile here, and adopts in place on the PR's own branch when GitHub permits ${yamlHint('governor.labels.stage.adoption.in_place')} (default on) rather than opening a superseding PR. An in-place adoption keeps a changed head only when GitHub links the commit to the App bot ${yamlHint('governor.labels.stage.adoption.strict_attribution')} (default on); otherwise the hive hands the PR back to the contributor. Once the hive owns the head, the contributor lane merges it under the hive's own authority when the hive-authored gates pass (CI green on the adopted head, reviewer verdict, DCO, diff fences, soak), without requiring the original author's trust tier ${yamlHint('governor.labels.stage.adoption.auto_merge')} (default on).
+
Go duration after the comment-only nudge before an offline or external author's red PR is adopted. With live hub presence the hive instead waits ${yamlHint('governor.labels.stage.adoption.after_offline')} (default 30m) after a registered relay's last heartbeat, or ${yamlHint('governor.labels.stage.adoption.after_unregistered')} (default 2h) for an author with no profile here, and adopts in place on the PR's own branch when GitHub permits ${yamlHint('governor.labels.stage.adoption.in_place')} (default on) rather than opening a superseding PR. An in-place adoption keeps a changed head only when GitHub links the commit to the App bot ${yamlHint('governor.labels.stage.adoption.strict_attribution')} (default on); otherwise the hive hands the PR back to the contributor. Once the hive owns the head, the contributor lane merges it under the hive's own authority when the hive-authored gates pass (CI green on the adopted head, reviewer verdict, DCO, diff fences, soak), without requiring the original author's trust tier ${yamlHint('governor.labels.stage.adoption.auto_merge')} (default on). When the hive supersedes instead, it closes the original in the same pass that opens its copy — one note naming the copy, hive/superseded — and reopens it if the copy is closed unmerged or red at two heads ${yamlHint('governor.labels.stage.adoption.supersede.close_first')} (default on; off keeps the original open at stage 074 until the copy merges).
${renderReporterTrustBlock()}

Writing guide

diff --git a/src/pkg/github/automerge/automerge_adoption_close_first.go b/src/pkg/github/automerge/automerge_adoption_close_first.go new file mode 100644 index 0000000000..0271d6ed86 --- /dev/null +++ b/src/pkg/github/automerge/automerge_adoption_close_first.go @@ -0,0 +1,285 @@ +package automerge + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Close-first supersession (hivecommons/hive#15135). +// +// A supersede-mode adoption used to open the hive's copy and leave the +// original open at 074 until the copy merged, so the operator saw two +// active PRs for one issue (#15029 and its copy #15123). Under +// governor.labels.stage.adoption.supersede.close_first (default on) the +// original is closed in the same pass the copy is opened — push branch → +// open copy → close original, so the one close note can name the copy — +// with the `hive/superseded` label, and the record reads superseded from +// the first render after the copy exists. +// +// The copy is then polled from the store (settleCloseFirstSupersessions: +// a closed original is not observed by any lane). Merged settles the +// record; closed unmerged, required checks red at adoptionCopyRedHeadsMax +// consecutive heads, or a branch the re-adoption could not push reopens the +// original with one note, drops the label and parks it at 085 with +// stagelabels.LaneReasonAdoptionCopyFailed. One reopen per original per +// stagelabels.AdoptionReopenWindow: a copy that keeps failing after that +// leaves the original closed with a note rather than flipping it. + +// AdoptionCopyFailedMarker opens the note posted when the hive reopens (or, +// past the reopen guard, declines to reopen) a close-first original. +const AdoptionCopyFailedMarker = "\n"+ + "Superseded by hive: re-authored as #%d with the same commits and sign-off because %s. "+ + "This PR is closed; if #%d fails, hive reopens it.", + AdoptionMarkerPrefix, nonEmpty(hiveID, "hive"), reason, stagelabels.AdoptModeSupersede, superseding, obs.HeadSHA, + superseding, adoptionCloseFirstCause(reason), superseding) +} + +// adoptionCopyFailedWhy is the plain-words clause of the reopen note. +func adoptionCopyFailedWhy(why string, redHeads int) string { + switch why { + case adoptionCopyFailedClosed: + return "closed without merging" + case adoptionCopyFailedRed: + return fmt.Sprintf("required checks red on %d heads", redHeads) + } + return why +} + +// AdoptionCopyFailedComment is the note posted on a reopened original. +func AdoptionCopyFailedComment(number, copy int, head, why string, redHeads int) string { + return fmt.Sprintf("%s superseded_by=%d head=%s why=%s -->\nHive's copy #%d failed (%s); reopening this PR; needs a person (`%s`).", + AdoptionCopyFailedMarker, copy, head, why, copy, adoptionCopyFailedWhy(why, redHeads), stagelabels.NeedsDecision.Label()) +} + +// AdoptionCopyFailedHeldComment is the note left when the reopen guard +// keeps a failed original closed: it was reopened for an earlier copy less +// than stagelabels.AdoptionReopenWindow ago. +func AdoptionCopyFailedHeldComment(copy int, head, why string, redHeads int, reopenedAt, now time.Time) string { + ago := now.Sub(reopenedAt).Round(time.Minute) + return fmt.Sprintf("%s superseded_by=%d head=%s why=%s held=true -->\nHive's copy #%d failed (%s). This PR was already reopened for an earlier copy %s ago, so the hive is not reopening it again; a person should decide what happens to it.", + AdoptionCopyFailedMarker, copy, head, why, copy, adoptionCopyFailedWhy(why, redHeads), ago) +} + +// closeSupersededOriginalFirst closes the original right after its copy +// opened. The comment goes first so GitHub shows why before the close; +// the record is marked as soon as the note is out so nothing re-posts it. +// A close GitHub refuses leaves the original open with the note (as the +// merge-time close does); any other close failure leaves the record on +// the keep-open path, where settleSuperseder closes it when the copy merges. +func (c *Engine) closeSupersededOriginalFirst(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, policy StageLabelPolicy, superseding int, reason, fence string, now time.Time) (int, error) { + owner, repo, number := obs.Owner, obs.Repo, obs.Number + logArgs := []any{"repo", obs.Display, "pr", number, "head", obs.HeadSHA, "superseded_by", superseding, "reason", reason} + body := AdoptionCloseFirstComment(obs, policy.HiveID, superseding, reason) + adoptionFenceNote(fence) + if _, _, err := c.gh.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { + c.warn("stage labels: close-first adoption note failed; original left open on the keep-open path", append(logArgs, "error", err)...) + return 0, err + } + if _, _, err := c.gh.Issues.AddLabelsToIssue(ctx, owner, repo, number, []string{hgithub.SupersededLabel}); err != nil { + c.warn("stage labels: close-first adoption label failed", append(logArgs, "label", hgithub.SupersededLabel, "error", err)...) + } + if _, _, err := c.gh.PullRequests.Edit(ctx, owner, repo, number, &gh.PullRequest{State: gh.Ptr("closed")}); err != nil { + if isGitHubStatus(err, http.StatusForbidden) || isGitHubStatus(err, http.StatusNotFound) { + store.MarkSupersededClosedFirst(key, superseding, true, now) + c.warn("stage labels: cannot close superseded original (permission); left open with the note", append(logArgs, "error", err)...) + return 1, nil + } + c.warn("stage labels: closing superseded original failed; it closes when the copy merges", append(logArgs, "error", err)...) + return 1, err + } + store.MarkSupersededClosedFirst(key, superseding, false, now) + c.info("stage labels: contributor PR closed in favour of the hive's copy", logArgs...) + return 1, nil +} + +// closeFirstRecord is one store record whose copy is due a poll, with +// the repository coordinates parsed from its key. +type closeFirstRecord struct { + key string + owner string + repo string + number int + rec stagelabels.Record +} + +// closeFirstPollCandidates lists the records whose close-first copy is due +// a poll, in key order. +func closeFirstPollCandidates(store *stagelabels.Store, now time.Time, interval time.Duration) []closeFirstRecord { + var out []closeFirstRecord + for _, key := range store.Keys() { + rec, ok := store.Get(key) + if !ok || !rec.CloseFirstPollDue(now, interval) { + continue + } + owner, repo, number, ok := splitStageKey(key) + if !ok { + continue + } + out = append(out, closeFirstRecord{key: key, owner: owner, repo: repo, number: number, rec: rec}) + } + return out +} + +// splitStageKey parses "owner/repo#n". +func splitStageKey(key string) (owner, repo string, number int, ok bool) { + full, n, found := strings.Cut(key, "#") + if !found { + return "", "", 0, false + } + owner, repo, found = strings.Cut(full, "/") + if !found || owner == "" || repo == "" { + return "", "", 0, false + } + number, err := strconv.Atoi(n) + if err != nil || number <= 0 { + return "", "", 0, false + } + return owner, repo, number, true +} + +// settleCloseFirstSupersessions polls every close-first copy on record (at +// most every adoptCheckInterval each) and settles its original: merged → +// the record is finished; closed unmerged, or red at +// adoptionCopyRedHeadsMax heads → the original is reopened. It returns the +// comments posted. Runs once per ApplyStageLabels pass, after the observed +// items, because a closed original is in no lane's listing. +func (c *Engine) settleCloseFirstSupersessions(ctx context.Context, store *stagelabels.Store, policy StageLabelPolicy, now time.Time) (int, error) { + if !policy.Adoption { + return 0, nil + } + comments := 0 + var firstErr error + for _, cand := range closeFirstPollCandidates(store, now, adoptCheckInterval) { + store.MarkAdoptChecked(cand.key, now) + n, err := c.settleCloseFirstCopy(ctx, store, cand, now) + comments += n + if err != nil && firstErr == nil { + firstErr = err + } + } + return comments, firstErr +} + +// settleCloseFirstCopy reads one copy's state and acts on it. +func (c *Engine) settleCloseFirstCopy(ctx context.Context, store *stagelabels.Store, cand closeFirstRecord, now time.Time) (int, error) { + rec, copy := cand.rec, cand.rec.AdoptedBy + display := cand.owner + "/" + cand.repo + logArgs := func(extra ...any) []any { + return append([]any{"repo", display, "pr", cand.number, "head", rec.AdoptedHead, "superseded_by", copy}, extra...) + } + sup, _, err := c.gh.PullRequests.Get(ctx, cand.owner, cand.repo, copy) + if err != nil { + c.warn("stage labels: close-first copy lookup failed", logArgs("error", err)...) + return 0, err + } + switch { + case sup.GetMerged(): + store.MarkCloseFirstSettled(cand.key) + c.info("stage labels: close-first copy merged; supersession settled", logArgs()...) + return 0, nil + case strings.EqualFold(sup.GetState(), "closed"): + return c.reopenSupersededOriginal(ctx, store, cand, adoptionCopyFailedClosed, 0, now) + } + // The copy's own stage record carries the lane's last reason for its + // head; red required checks at a second head mean the fixer's repair + // did not take. + copyRec, ok := store.Get(stagelabels.Key(display, copy)) + head := sup.GetHead().GetSHA() + if !ok || head == "" || adoptionFailingCheck(copyRec.Reason) == "" || copyRec.ActivityHead != head { + return 0, nil + } + redHeads := store.NoteAdoptCopyRedHead(cand.key, head) + if redHeads < adoptionCopyRedHeadsMax { + c.info("stage labels: close-first copy red", logArgs("copy_head", head, "copy_reason", copyRec.Reason, "red_heads", redHeads)...) + return 0, nil + } + return c.reopenSupersededOriginal(ctx, store, cand, adoptionCopyFailedRed, redHeads, now) +} + +// reopenSupersededOriginal gives a close-first original back to a person: +// reopen, one note, `hive/superseded` off, parked at 085 with +// LaneReasonAdoptionCopyFailed. Within stagelabels.AdoptionReopenWindow of +// the last reopen the original stays closed and the note says so. +func (c *Engine) reopenSupersededOriginal(ctx context.Context, store *stagelabels.Store, cand closeFirstRecord, why string, redHeads int, now time.Time) (int, error) { + rec, copy := cand.rec, cand.rec.AdoptedBy + owner, repo, number := cand.owner, cand.repo, cand.number + logArgs := []any{"repo", owner + "/" + repo, "pr", number, "head", rec.AdoptedHead, "superseded_by", copy, "why", why, "red_heads", redHeads} + if rec.AdoptionReopenedWithin(now, stagelabels.AdoptionReopenWindow) { + store.MarkCloseFirstSettled(cand.key) + c.warn("stage labels: close-first copy failed again within the reopen window; original stays closed", append(logArgs, "reopened_at", rec.AdoptReopenedAt)...) + body := AdoptionCopyFailedHeldComment(copy, rec.AdoptedHead, why, redHeads, rec.AdoptReopenedAt, now) + if _, _, err := c.gh.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { + c.warn("stage labels: close-first held note failed", append(logArgs, "error", err)...) + return 0, err + } + return 1, nil + } + if _, _, err := c.gh.PullRequests.Edit(ctx, owner, repo, number, &gh.PullRequest{State: gh.Ptr("open")}); err != nil { + c.warn("stage labels: reopening superseded original failed", append(logArgs, "error", err)...) + return 0, err + } + // The reopen is out: record it before the note so a failed comment + // never reopens twice. + store.MarkAdoptionReopened(cand.key, copy, now) + store.MarkAdoptionEscalated(cand.key, rec.AdoptedHead, stagelabels.LaneReasonAdoptionCopyFailed) + c.warn("stage labels: hive's copy failed; original reopened for a person", logArgs...) + comments := 0 + var firstErr error + body := AdoptionCopyFailedComment(number, copy, rec.AdoptedHead, why, redHeads) + if _, _, err := c.gh.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { + c.warn("stage labels: reopen note failed", append(logArgs, "error", err)...) + firstErr = err + } else { + comments++ + } + if _, err := c.gh.Issues.RemoveLabelForIssue(ctx, owner, repo, number, url.PathEscape(hgithub.SupersededLabel)); err != nil && !isGitHubStatus(err, http.StatusNotFound) { + c.warn("stage labels: removing superseded label failed", append(logArgs, "label", hgithub.SupersededLabel, "error", err)...) + if firstErr == nil { + firstErr = err + } + } + return comments, firstErr +} diff --git a/src/pkg/github/automerge/automerge_adoption_close_first_test.go b/src/pkg/github/automerge/automerge_adoption_close_first_test.go new file mode 100644 index 0000000000..a672af8275 --- /dev/null +++ b/src/pkg/github/automerge/automerge_adoption_close_first_test.go @@ -0,0 +1,366 @@ +package automerge + +import ( + "context" + "net/http" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/delegatedecision" + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Close-first supersession (hivecommons/hive#15135): the original closes as +// soon as the hive's copy exists, and is reopened if the copy fails. + +func closeFirstPolicy() StageLabelPolicy { + p := adoptPolicy() + p.AdoptSupersedeCloseFirst = true + return p +} + +// closeFirstHarness is the adopt harness with every write to the fake API +// logged in order (method + path), across both the stage and adopt routes. +type closeFirstHarness struct { + *adoptHarness + writes []string +} + +func newCloseFirstHarness(t *testing.T, policy StageLabelPolicy) (*closeFirstHarness, func()) { + t.Helper() + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, policy) + cf := &closeFirstHarness{adoptHarness: h} + inner := h.api.extra + h.api.extra = func(w http.ResponseWriter, r *http.Request) bool { + if r.Method != http.MethodGet { + cf.writes = append(cf.writes, r.Method+" "+r.URL.Path) + } + return inner(w, r) + } + return cf, done +} + +// adoptCloseFirst runs #7 (head aaa) through nudge → grace → adoption and +// returns the result of the adopting pass. +func (h *closeFirstHarness) adoptCloseFirst() StageLabelResult { + h.t.Helper() + 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.api.reset() + h.writes = nil + return h.observe(adoptPR(7, "aaa", labels...), "check-failing:unit") +} + +// sweep runs a pass with no observation of #7 — a closed original is in no +// lane's listing. +func (h *closeFirstHarness) sweep() StageLabelResult { + h.t.Helper() + res, err := h.e.ApplyStageLabels(context.Background()) + if err != nil { + h.t.Fatal(err) + } + return res +} + +func (h *closeFirstHarness) record() stagelabels.Record { + rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) + return rec +} + +func writesMatching(writes []string, want ...string) []string { + var out []string + for _, w := range writes { + for _, m := range want { + if strings.HasPrefix(w, m) { + out = append(out, w) + break + } + } + } + return out +} + +func TestAdoptionCloseFirstOrdering(t *testing.T) { + h, done := newCloseFirstHarness(t, closeFirstPolicy()) + defer done() + res := h.adoptCloseFirst() + if len(h.adopt.pulls) != 1 { + t.Fatalf("pulls=%d", len(h.adopt.pulls)) + } + // push branch → open copy → note → label → close, in one pass. + got := writesMatching(h.writes, "POST /repos/acme/widget/git/refs", "POST /repos/acme/widget/pulls", "POST /repos/acme/widget/issues/7/comments", "POST /repos/acme/widget/issues/7/labels", "PATCH /repos/acme/widget/pulls/7") + want := []string{"POST /repos/acme/widget/git/refs", "POST /repos/acme/widget/pulls", "POST /repos/acme/widget/issues/7/comments", "POST /repos/acme/widget/issues/7/labels", "PATCH /repos/acme/widget/pulls/7"} + // The lane's own 074 label on the original follows; nothing else does. + if len(got) < len(want) || strings.Join(got[:len(want)], "\n") != strings.Join(want, "\n") || len(writesMatching(got[len(want):], "PATCH", "POST /repos/acme/widget/issues/7/comments")) != 0 { + t.Fatalf("write order:\n%s\nall: %v", strings.Join(got, "\n"), h.writes) + } + if len(h.adopt.prEdits) != 1 || h.adopt.prEdits[0] != "7:closed" { + t.Fatalf("original not closed: %v", h.adopt.prEdits) + } + if res.Comments < 1 { + t.Fatalf("res=%+v", res) + } + if added := h.addedLabels(); len(added) != 2 || added[0] != hgithub.SupersededLabel || added[1] != "stage/074-adopted-by-hive" { + t.Fatalf("superseded label: %v", h.api.added) + } + rec := h.record() + if !rec.SupersededClosedFirst || rec.SupersededBy != 90 || rec.AdoptedBy != 90 || rec.SupersededOpen || !rec.Superseded() { + t.Fatalf("record = %+v", rec) + } + if !rec.CloseFirstPollDue(h.clock.now().Add(adoptCheckInterval), adoptCheckInterval) || rec.SupersederPollDue(h.clock.now().Add(adoptCheckInterval), adoptCheckInterval) { + t.Fatalf("poll gates: %+v", rec) + } +} + +func TestAdoptionCloseFirstCommentText(t *testing.T) { + obs := stageObservation{Owner: "acme", Repo: "widget", Number: 7, HeadSHA: "aaa"} + cases := []struct { + name, reason, want string + }{ + {"grace", AdoptReasonGrace, "because CI stayed red and the author was unavailable"}, + {"offline", AdoptReasonOffline, "because the author's clanker is offline"}, + {"unregistered", AdoptReasonUnregistered, "because the author has no registered clanker here and is not a trusted author"}, + {"operator", AdoptReasonOperator, "because an operator asked for it"}, + {"rehome", AdoptReasonDCORehome, "because its commits needed a sign-off"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + body := AdoptionCloseFirstComment(obs, "acme-hive", 90, tc.reason) + for _, want := range []string{AdoptionMarkerPrefix, "mode=supersede", "superseded_by=90", "head=aaa", "close_first=true", "Superseded by hive: re-authored as #90 with the same commits and sign-off ", tc.want, "This PR is closed; if #90 fails, hive reopens it."} { + if !strings.Contains(body, want) { + t.Fatalf("missing %q in\n%s", want, body) + } + } + if strings.Contains(body, "closed automatically once") { + t.Fatalf("keep-open wording in close-first note:\n%s", body) + } + }) + } +} + +func TestAdoptionCloseFirstOffKeepsOriginalOpen(t *testing.T) { + h, done := newCloseFirstHarness(t, adoptPolicy()) + defer done() + h.adoptCloseFirst() + if len(h.adopt.prEdits) != 0 { + t.Fatalf("closed with close_first off: %v", h.adopt.prEdits) + } + if rec := h.record(); rec.SupersededClosedFirst || rec.Superseded() { + t.Fatalf("record = %+v", rec) + } +} + +func TestAdoptionCloseFirstCloseRefusedLeavesOpenNoted(t *testing.T) { + h, done := newCloseFirstHarness(t, closeFirstPolicy()) + defer done() + h.adopt.failEditStatus = http.StatusForbidden + h.adoptCloseFirst() + rec := h.record() + if !rec.SupersededClosedFirst || !rec.SupersededOpen || rec.SupersededBy != 90 { + t.Fatalf("record = %+v", rec) + } + if c := adoptionComments(h.api); len(c) != 1 || !strings.Contains(c[0], "close_first=true") { + t.Fatalf("comments: %v", h.api.comments) + } +} + +func TestAdoptionCloseFirstCopyMergedSettles(t *testing.T) { + h, done := newCloseFirstHarness(t, closeFirstPolicy()) + defer done() + h.adoptCloseFirst() + h.adopt.setSuperseder(90, "closed", true) + h.api.reset() + h.writes = nil + h.clock.advance(adoptCheckInterval) + res := h.sweep() + if h.api.writes() != 0 || len(h.adopt.prEdits) != 1 || res.Comments != 0 { + t.Fatalf("writes on a merged copy: %v %v res=%+v", h.writes, h.api.comments, res) + } + rec := h.record() + if rec.SupersededClosedFirst || !rec.Superseded() || rec.SupersededBy != 90 { + t.Fatalf("record = %+v", rec) + } + h.clock.advance(adoptCheckInterval) + h.sweep() + if len(h.writes) != 0 { + t.Fatalf("polled after settling: %v", h.writes) + } +} + +func TestAdoptionCloseFirstReopenOnFailure(t *testing.T) { + cases := []struct { + name string + // fail arranges the copy's failure after adoption; it returns the + // number of sweeps (adoptCheckInterval apart) that must run. + fail func(h *closeFirstHarness) int + why string + wantNote string + }{ + { + name: "copy closed unmerged", + fail: func(h *closeFirstHarness) int { + h.adopt.setSuperseder(90, "closed", false) + return 1 + }, + why: adoptionCopyFailedClosed, + wantNote: "closed without merging", + }, + { + name: "copy red at two heads", + fail: func(h *closeFirstHarness) int { + h.adopt.setSupersederHead(90, "open", false, "c1") + h.e.recordStage(stageLaneSelf, "acme/widget", "acme", "widget", nudgePR(90, "c1"), "required-check-failing:unit", false, nil) + h.sweep() // first red head: noted, no reopen + if len(h.adopt.prEdits) != 1 { + h.t.Fatalf("reopened at the first red head: %v", h.adopt.prEdits) + } + h.clock.advance(adoptCheckInterval) + h.sweep() // same head again: still one + if rec := h.record(); len(rec.AdoptCopyRedHeads) != 1 || len(h.adopt.prEdits) != 1 { + h.t.Fatalf("same red head counted twice: %+v edits=%v", rec, h.adopt.prEdits) + } + h.clock.advance(adoptCheckInterval) + h.adopt.setSupersederHead(90, "open", false, "c2") + h.e.recordStage(stageLaneSelf, "acme/widget", "acme", "widget", nudgePR(90, "c2"), "required-check-failing:unit", false, nil) + return 1 + }, + why: adoptionCopyFailedRed, + wantNote: "required checks red on 2 heads", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + h, done := newCloseFirstHarness(t, closeFirstPolicy()) + defer done() + h.adoptCloseFirst() + h.api.reset() + h.writes = nil + h.clock.advance(adoptCheckInterval) + sweeps := tc.fail(h) + h.api.reset() + h.writes = nil + var res StageLabelResult + for i := 0; i < sweeps; i++ { + res = h.sweep() + } + if len(h.adopt.prEdits) != 2 || h.adopt.prEdits[1] != "7:open" { + t.Fatalf("original not reopened: %v", h.adopt.prEdits) + } + notes := commentsWithPrefix(h.api, AdoptionCopyFailedMarker) + if len(notes) != 1 || res.Comments != 1 { + t.Fatalf("reopen note: %v res=%+v", h.api.comments, res) + } + for _, want := range []string{"Hive's copy #90 failed (", tc.wantNote, "reopening this PR; needs a person", "why=" + tc.why} { + if !strings.Contains(notes[0], want) { + t.Fatalf("note missing %q:\n%s", want, notes[0]) + } + } + if len(h.api.removed) != 1 || h.api.removed[0] != hgithub.SupersededLabel { + t.Fatalf("superseded label not removed: %v", h.api.removed) + } + // reopen → note → label off. + got := writesMatching(h.writes, "PATCH /repos/acme/widget/pulls/7", "POST /repos/acme/widget/issues/7/comments", "DELETE /repos/acme/widget/issues/7/labels/") + if len(got) != 3 || !strings.HasPrefix(got[0], "PATCH") || !strings.HasPrefix(got[1], "POST") || !strings.HasPrefix(got[2], "DELETE") { + t.Fatalf("reopen write order: %v", got) + } + rec := h.record() + if rec.Superseded() || rec.SupersededClosedFirst || !rec.AdoptionCopyFailed() || rec.AdoptReopenedFrom != 90 || rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionCopyFailed || !rec.AdoptionEscalated("aaa") { + t.Fatalf("record after reopen = %+v", rec) + } + // The reopened original comes back into the lane: parked at 085 + // with the copy-failed reason, no fresh copy, no second reopen. + h.api.reset() + h.writes = nil + h.clock.advance(adoptCheckInterval) + 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/085-needs-decision" { + t.Fatalf("085 after reopen: added=%v removed=%v", h.api.added, h.api.removed) + } + if len(h.adopt.pulls) != 1 || len(h.adopt.prEdits) != 2 { + t.Fatalf("re-adopted after reopen: pulls=%d edits=%v", len(h.adopt.pulls), h.adopt.prEdits) + } + }) + } +} + +func TestAdoptionCloseFirstReopenPingPongGuard(t *testing.T) { + h, done := newCloseFirstHarness(t, closeFirstPolicy()) + defer done() + h.adoptCloseFirst() + h.adopt.setSuperseder(90, "closed", false) + h.clock.advance(adoptCheckInterval) + h.sweep() + if len(h.adopt.prEdits) != 2 || h.adopt.prEdits[1] != "7:open" { + t.Fatalf("first reopen: %v", h.adopt.prEdits) + } + // An operator asks again within the window: a fresh copy, closed + // first; when that copy fails too the original stays closed and the + // note says why. + h.api.reset() + h.clock.advance(time.Hour) + h.observe(adoptPR(7, "aaa", "stage/085-needs-decision", "role/contributor", "author/clanker", delegatedecision.LabelAdoptSupersede), "check-failing:unit") + if len(h.adopt.pulls) != 2 || len(h.adopt.prEdits) != 3 || h.adopt.prEdits[2] != "7:closed" { + t.Fatalf("operator re-adoption: pulls=%d edits=%v comments=%v", len(h.adopt.pulls), h.adopt.prEdits, h.api.comments) + } + if rec := h.record(); !rec.SupersededClosedFirst || rec.SupersededBy != 91 || rec.AdoptReopenedAt.IsZero() { + t.Fatalf("record after re-adoption = %+v", rec) + } + h.adopt.setSuperseder(91, "closed", false) + h.api.reset() + h.clock.advance(adoptCheckInterval) + res := h.sweep() + if len(h.adopt.prEdits) != 3 { + t.Fatalf("reopened twice within %v: %v", stagelabels.AdoptionReopenWindow, h.adopt.prEdits) + } + notes := commentsWithPrefix(h.api, AdoptionCopyFailedMarker) + if len(notes) != 1 || res.Comments != 1 || !strings.Contains(notes[0], "#91") || !strings.Contains(notes[0], "not reopening it again") || !strings.Contains(notes[0], "held=true") { + t.Fatalf("held note: %v res=%+v", h.api.comments, res) + } + if len(h.api.removed) != 0 { + t.Fatalf("label removed while held: %v", h.api.removed) + } + rec := h.record() + if rec.SupersededClosedFirst || !rec.Superseded() || rec.SupersededBy != 91 { + t.Fatalf("record after held = %+v", rec) + } + // Past the window the guard no longer applies. + if !rec.AdoptionReopenedWithin(h.clock.now(), stagelabels.AdoptionReopenWindow) || rec.AdoptionReopenedWithin(h.clock.now().Add(stagelabels.AdoptionReopenWindow), stagelabels.AdoptionReopenWindow) { + t.Fatalf("reopen window: %+v now=%v", rec, h.clock.now()) + } +} + +func TestAdoptionCopyFailedMarkerIsSettled(t *testing.T) { + if !AdoptionSettledMarker(AdoptionCopyFailedComment(7, 90, "aaa", adoptionCopyFailedClosed, 0)) { + t.Fatal("reopen note not a settled marker: the marker scan would re-adopt") + } +} + +func TestSplitStageKey(t *testing.T) { + cases := []struct { + key string + owner, repo string + number int + ok bool + }{ + {"acme/widget#7", "acme", "widget", 7, true}, + {"acme/widget", "", "", 0, false}, + {"widget#7", "", "", 0, false}, + {"acme/widget#x", "", "", 0, false}, + {"acme/widget#0", "", "", 0, false}, + } + for _, tc := range cases { + owner, repo, number, ok := splitStageKey(tc.key) + if owner != tc.owner || repo != tc.repo || number != tc.number || ok != tc.ok { + t.Errorf("splitStageKey(%q) = %q %q %d %v", tc.key, owner, repo, number, ok) + } + } +} + +func TestAdoptReasonOperatorText(t *testing.T) { + if !strings.Contains(adoptReasonText(AdoptReasonOperator, time.Hour, false), "maintainer asked") || !strings.Contains(adoptionBodyCause(AdoptReasonOperator), "maintainer's request") { + t.Fatalf("%q / %q", adoptReasonText(AdoptReasonOperator, time.Hour, false), adoptionBodyCause(AdoptReasonOperator)) + } +} diff --git a/src/pkg/github/automerge/automerge_adoption_orphan.go b/src/pkg/github/automerge/automerge_adoption_orphan.go index 6a7e4eae13..361011d2dd 100644 --- a/src/pkg/github/automerge/automerge_adoption_orphan.go +++ b/src/pkg/github/automerge/automerge_adoption_orphan.go @@ -103,7 +103,7 @@ func ParseAdoptionMarker(body string) (AdoptionMarker, bool) { // 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} { + for _, prefix := range []string{adoptionCloseMarker, AdoptionRevertedMarker, AdoptionChildClosedTwiceMarker, AdoptionCopyFailedMarker, AdoptionYieldMarker, AdoptionSupersederMergedDivergedMarker, AdoptionMaintainerSupersederMarker} { if strings.HasPrefix(body, prefix) { return true } diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index 018bc46c01..1fadf0380e 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -98,6 +98,9 @@ const ( AdoptReasonOffline = "contributor-offline" AdoptReasonUnregistered = "contributor-unregistered" AdoptReasonGrace = "grace-elapsed" + // AdoptReasonOperator is an owner-delegate's `adoption/supersede` + // request: no nudge or grace, the operator asked (hivecommons/hive#15135). + AdoptReasonOperator = "operator-asked" // AdoptReasonDCORehome marks a second takeover: an in-place adoption // whose head stayed DCO-red with unsigned commits the hive cannot // rewrite on the contributor branch, re-homed onto hive/adopt-. @@ -193,6 +196,8 @@ func adoptReasonText(reason string, grace time.Duration, online bool) string { return "your hive has been offline since the nudge" case AdoptReasonUnregistered: return "no hive of yours is registered here and nothing moved after the nudge" + case AdoptReasonOperator: + return "a maintainer asked the hive to take it over" case AdoptReasonSignoffRehome: return "the hive's fix on this branch cannot carry a sign-off the squash merge accepts (the hive signs as the App, the squash lands as you)" case AdoptReasonRebaseRehome: @@ -420,6 +425,8 @@ func adoptionBodyCause(reason string) string { return "adopted in place, re-homed because a review finding on the adopted head sat unaddressed past the stage dwell" case AdoptReasonDCORehome: return "adopted in place, re-homed for DCO repair" + case AdoptReasonOperator: + return "adopted at a maintainer's request" } return "adopted after CI went red and the contributor was unavailable" } @@ -580,10 +587,10 @@ func (c *Engine) adoptContributorPR(ctx context.Context, store *stagelabels.Stor } requested := hasLabel(obs.Labels, delegatedecision.LabelAdoptSupersede) due, reason := adoptionDue(rec, obs.HeadSHA, policy, now, presence, hasPresence) - if requested && obs.HeadSHA != "" && !(rec.Adopted(obs.HeadSHA) && rec.AdoptedBy > 0) { + if requested && obs.HeadSHA != "" && !(rec.Adopted(obs.HeadSHA) && rec.AdoptedBy > 0 && !rec.AdoptionCopyFailed()) { // An explicit owner-delegate decision bypasses the nudge/grace and // re-homes a stalled in-place adoption through the same supersede path. - due, reason = true, AdoptReasonOffline + due, reason = true, AdoptReasonOperator } if !due { return 0, nil @@ -739,6 +746,8 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor return effects.Result{Provenance: tip}, nil }) if err != nil { + // Close-first (hivecommons/hive#15135) orders push → copy → close, + // so a failed push leaves the original open; nothing to reopen. c.warn("stage labels: adoption failed to push branch", "repo", obs.Display, "pr", number, "branch", branch, "error", err) return 0, err } @@ -768,6 +777,11 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor store.MarkAdopted(key, obs.HeadSHA, superseding, now) c.info("stage labels: contributor PR adopted", "repo", obs.Display, "pr", number, "head", obs.HeadSHA, "superseded_by", superseding, "branch", branch, "fence", rec.AdoptDiffFence) + if policy.AdoptSupersedeCloseFirst { + // hivecommons/hive#15135: one PR per issue — the original closes + // now, with the note naming the copy, not when the copy merges. + return c.closeSupersededOriginalFirst(ctx, store, key, obs, policy, superseding, reason, rec.AdoptDiffFence, now) + } body := AdoptionComment(obs, policy.HiveID, superseding, reason, grace, online) if dcoRehome { body = AdoptionDCORehomeComment(obs, policy.HiveID, superseding) @@ -993,7 +1007,7 @@ func (c *Engine) settleAdoption(ctx context.Context, store *stagelabels.Store, k // 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) { + if obs.Merged || rec.AdoptionChildClosedTwice() || rec.AdoptionCopyFailed() || !rec.SupersederPollDue(now, adoptCheckInterval) { return 0, nil } store.MarkAdoptChecked(key, now) @@ -1573,7 +1587,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 || rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionChildClosedTwice || (rehomeReason == "" && adoptionStalledReason(obs, rec, policy, now) != "") { + if rec.AdoptEscalatedReason == adoptionWorkflowRefusedReason || rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionChildClosedTwice || rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionCopyFailed || (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_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 7a359b6c42..c8c3aeaa40 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -93,6 +93,12 @@ type StageLabelPolicy struct { // author holds admin or maintain on the repository. "" reads as the // default, adopt (hivecommons/hive#14524). AdoptionMaintainerPolicy string + // AdoptSupersedeCloseFirst closes the original as soon as the hive's + // superseding copy is open and reopens it if the copy fails + // (governor.labels.stage.adoption.supersede.close_first, default on; + // hivecommons/hive#15135). Off, the original stays open at 074 until + // the copy merges. + AdoptSupersedeCloseFirst bool // DCORequired resolves the per-repo `governor.labels.stage.dco.required` // override for "owner/repo"; ok false means "detect" (a DCO context in // the branch's required checks or on the head). nil = always detect. @@ -1249,6 +1255,14 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) // The hive's own comments are not outside activity. store.CountOwnComments(key, result.Comments-commentsBefore) } + // hivecommons/hive#15135: a close-first original is in no lane's + // listing; its copy is polled from the store. + if n, err := c.settleCloseFirstSupersessions(ctx, store, policy, now); true { + result.Comments += n + if err != nil && firstErr == nil { + firstErr = err + } + } store.Prune(stageStorePruneAge) if err := store.Save(); err != nil { c.warn("stage labels: persisting state failed", "error", err) diff --git a/src/pkg/stagelabels/adoption_close_first_test.go b/src/pkg/stagelabels/adoption_close_first_test.go new file mode 100644 index 0000000000..1cbc777c08 --- /dev/null +++ b/src/pkg/stagelabels/adoption_close_first_test.go @@ -0,0 +1,119 @@ +package stagelabels + +import ( + "path/filepath" + "testing" + "time" +) + +// Close-first supersession memory (hivecommons/hive#15135): the original is +// superseded the moment its copy exists, the copy is polled on its own gate, +// red copy heads are counted once each, and a reopen starts the 24h window. +func TestStoreCloseFirstSupersession(t *testing.T) { + now := time.Date(2026, 10, 29, 9, 0, 0, 0, time.UTC) + st := NewStore(filepath.Join(t.TempDir(), "s.json"), func() time.Time { return now }) + key := Key("hivecommons/hive", 15029) + interval := 10 * time.Minute + + // Unknown key or no copy: no-op. + st.MarkSupersededClosedFirst(key, 15123, false, now) + st.MarkCloseFirstSettled(key) + st.MarkAdoptionReopened(key, 15123, now) + if st.NoteAdoptCopyRedHead(key, "c1") != 0 { + t.Fatal("red head on an unknown key") + } + if _, ok := st.Get(key); ok { + t.Fatal("note created a record") + } + st.Observe(key, AdoptedByHive, "") + st.MarkAdopted(key, "aaa", 15123, now) + st.MarkSupersededClosedFirst(key, 0, false, now) + if r, _ := st.Get(key); r.SupersededClosedFirst || r.Superseded() { + t.Fatalf("zero copy recorded: %+v", r) + } + + st.MarkSupersededClosedFirst(key, 15123, false, now) + r, _ := st.Get(key) + if !r.SupersededClosedFirst || r.SupersededBy != 15123 || !r.SupersededAt.Equal(now) || r.SupersededOpen || !r.Superseded() || r.AdoptionCopyFailed() { + t.Fatalf("close-first not recorded: %+v", r) + } + // Superseded: the keep-open poll is off, the close-first poll is on + // (the adoption's MarkAdoptChecked gates it, from the adoption time). + if r.SupersederPollDue(now.Add(interval), interval) { + t.Fatal("keep-open poll still due") + } + if r.CloseFirstPollDue(now.Add(interval-time.Second), interval) || !r.CloseFirstPollDue(now.Add(interval), interval) { + t.Fatal("close-first poll interval misread") + } + if (Record{SupersededClosedFirst: true, AdoptedBy: 1, SupersededBy: 1}).CloseFirstPollDue(now, interval) != true || (Record{SupersededClosedFirst: true, AdoptedBy: 1, SupersededBy: 2}).CloseFirstPollDue(now, interval) { + t.Fatal("close-first poll gate misread") + } + // A close GitHub refused: superseded but open. + st.MarkSupersededClosedFirst(key, 15123, true, now.Add(time.Minute)) + if r, _ = st.Get(key); !r.SupersededOpen || !r.SupersededAt.Equal(now) { + t.Fatalf("refused close: %+v", r) + } + + // Red copy heads: distinct heads count, repeats and blanks do not. + for i, tc := range []struct { + head string + want int + }{{"c1", 1}, {"c1", 1}, {" ", 1}, {"c2", 2}, {"c2", 2}} { + if got := st.NoteAdoptCopyRedHead(key, tc.head); got != tc.want { + t.Fatalf("red head %d (%q) = %d, want %d", i, tc.head, got, tc.want) + } + } + + // Copy merged: settled, still superseded, no more polling. + st.MarkCloseFirstSettled(key) + r, _ = st.Get(key) + if r.SupersededClosedFirst || len(r.AdoptCopyRedHeads) != 0 || !r.Superseded() || r.CloseFirstPollDue(now.Add(time.Hour), interval) { + t.Fatalf("settled: %+v", r) + } + st.MarkCloseFirstSettled(key) // idempotent + + // Reopen: the supersession is forgotten, the adoption stays (one + // attempt per head), the window starts, the park reads as copy-failed. + st.MarkSupersededClosedFirst(key, 15123, false, now) + st.NoteAdoptCopyRedHead(key, "c1") + reopened := now.Add(2 * time.Hour) + st.MarkAdoptionReopened(key, 15123, reopened) + st.MarkAdoptionEscalated(key, "aaa", LaneReasonAdoptionCopyFailed) + r, _ = st.Get(key) + if r.Superseded() || r.SupersededClosedFirst || r.SupersededOpen || !r.SupersededAt.IsZero() || len(r.AdoptCopyRedHeads) != 0 { + t.Fatalf("reopen left supersession: %+v", r) + } + if r.AdoptedBy != 15123 || r.AdoptedHead != "aaa" || r.AdoptReopenedFrom != 15123 || !r.AdoptReopenedAt.Equal(reopened) || !r.AdoptCheckedAt.Equal(reopened) { + t.Fatalf("reopen memory: %+v", r) + } + if !r.AdoptionCopyFailed() || !r.AdoptionEscalated("aaa") { + t.Fatalf("copy-failed park not read: %+v", r) + } + if !r.AdoptionReopenedWithin(reopened.Add(AdoptionReopenWindow-time.Minute), AdoptionReopenWindow) || r.AdoptionReopenedWithin(reopened.Add(AdoptionReopenWindow), AdoptionReopenWindow) { + t.Fatal("reopen window misread") + } + if (Record{}).AdoptionReopenedWithin(now, AdoptionReopenWindow) { + t.Fatal("zero record read as reopened") + } + // Another reason, or a different copy, is not the copy-failed park. + st.MarkAdoptionEscalated(key, "aaa", LaneReasonAdoptionChildClosedTwice) + if r, _ = st.Get(key); r.AdoptionCopyFailed() { + t.Fatal("another reason read as copy-failed") + } + st.MarkAdoptionEscalated(key, "aaa", LaneReasonAdoptionCopyFailed) + st.MarkAdopted(key, "aaa", 15200, reopened.Add(time.Hour)) + r, _ = st.Get(key) + if r.AdoptionCopyFailed() { + t.Fatalf("fresh copy still read as failed: %+v", r) + } + // A fresh adoption keeps the reopen time (the window outlives the copy). + if !r.AdoptReopenedAt.Equal(reopened) { + t.Fatalf("reopen time lost on re-adoption: %+v", r) + } + // ClearAdoption keeps the reopen time too but forgets the rest. + st.ClearAdoption(key) + r, _ = st.Get(key) + if r.SupersededClosedFirst || r.AdoptReopenedFrom != 0 || len(r.AdoptCopyRedHeads) != 0 || !r.AdoptReopenedAt.Equal(reopened) { + t.Fatalf("ClearAdoption: %+v", r) + } +} diff --git a/src/pkg/stagelabels/store.go b/src/pkg/stagelabels/store.go index 2aad363c8d..636af91db3 100644 --- a/src/pkg/stagelabels/store.go +++ b/src/pkg/stagelabels/store.go @@ -86,6 +86,20 @@ type Record struct { SupersededBy int `json:"superseded_by,omitempty"` SupersededAt time.Time `json:"superseded_at,omitempty"` SupersededOpen bool `json:"superseded_open,omitempty"` + // SupersededClosedFirst is true while the original was closed at + // adoption time, before its copy merged (close-first supersession, + // hivecommons/hive#15135). The reconciler keeps polling the copy + // (CloseFirstPollDue): merged clears the flag and the record reads as + // an ordinary supersession; closed unmerged, red across two heads, or a + // branch that cannot be pushed reopens the original + // (MarkAdoptionReopened). AdoptCopyRedHeads are the copy heads seen red + // so far; AdoptReopenedAt / AdoptReopenedFrom record the last reopen + // and the copy that failed, so one original is reopened at most once + // per AdoptionReopenWindow. + SupersededClosedFirst bool `json:"superseded_closed_first,omitempty"` + AdoptCopyRedHeads []string `json:"adopt_copy_red_heads,omitempty"` + AdoptReopenedAt time.Time `json:"adopt_reopened_at,omitempty"` + AdoptReopenedFrom int `json:"adopt_reopened_from,omitempty"` // SupersederMergedNotedHead is the original's head for which the // "superseder merged but this PR has moved on" note was posted, so a // diverged original is told once per head rather than every poll. @@ -1311,6 +1325,7 @@ func (s *Store) ClearAdoption(key string) { 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, "" + r.SupersededClosedFirst, r.AdoptCopyRedHeads, r.AdoptReopenedFrom = false, nil, 0 s.items[key] = r s.dirty = true } @@ -1352,11 +1367,124 @@ func (s *Store) MarkSupersederMergedNoted(key, head string) { } // Superseded reports whether the adoption's superseding PR merged and the -// original was told: the row is finished whatever its labels say. +// original was told — or, under close-first supersession, the original was +// closed in favour of a copy still in flight: the row is finished whatever +// its labels say. func (r Record) Superseded() bool { return r.SupersededBy > 0 } +// AdoptionReopenWindow is how long after a reopen-on-failure the same +// original is not reopened again (hivecommons/hive#15135): a copy that +// keeps failing must not flip the original closed/open every sweep. +const AdoptionReopenWindow = 24 * time.Hour + +// LaneReasonAdoptionCopyFailed is the 085 reason for an original the hive +// reopened because its close-first copy was closed unmerged, stayed red +// across two heads, or could not be pushed: a person must decide. +const LaneReasonAdoptionCopyFailed = "adoption-copy-failed" + +// MarkSupersededClosedFirst records that the original was closed as soon +// as superseding PR `by` existed (close-first supersession). The copy is +// still polled; see CloseFirstPollDue. open is true when GitHub refused +// the close and the original stays open with the note. +func (s *Store) MarkSupersededClosedFirst(key string, by int, open bool, now time.Time) { + s.mu.Lock() + defer s.mu.Unlock() + r, ok := s.items[key] + if !ok || by <= 0 { + return + } + if r.SupersededBy != by { + r.SupersededAt = now + } + r.SupersededBy, r.SupersededOpen, r.SupersededClosedFirst = by, open, true + r.AdoptCopyRedHeads = nil + s.items[key] = r + s.dirty = true +} + +// MarkCloseFirstSettled ends the polling of a close-first supersession: +// the copy merged (the record reads as an ordinary finished supersession) +// or the reopen guard left the original closed for a person. +func (s *Store) MarkCloseFirstSettled(key string) { + s.mu.Lock() + defer s.mu.Unlock() + r, ok := s.items[key] + if !ok || !r.SupersededClosedFirst { + return + } + r.SupersededClosedFirst, r.AdoptCopyRedHeads = false, nil + s.items[key] = r + s.dirty = true +} + +// NoteAdoptCopyRedHead records that the copy's required checks were red at +// head and returns how many distinct red heads the copy has shown. +func (s *Store) NoteAdoptCopyRedHead(key, head string) int { + s.mu.Lock() + defer s.mu.Unlock() + r, ok := s.items[key] + if !ok { + return 0 + } + head = strings.TrimSpace(head) + if head == "" { + return len(r.AdoptCopyRedHeads) + } + for _, h := range r.AdoptCopyRedHeads { + if h == head { + return len(r.AdoptCopyRedHeads) + } + } + r.AdoptCopyRedHeads = append(r.AdoptCopyRedHeads, head) + s.items[key] = r + s.dirty = true + return len(r.AdoptCopyRedHeads) +} + +// MarkAdoptionReopened records that the original was reopened because copy +// `from` failed: the supersession is forgotten, the adoption record stays +// (one attempt per head) and the reopen time starts AdoptionReopenWindow. +// The caller parks the head with MarkAdoptionEscalated. +func (s *Store) MarkAdoptionReopened(key string, from int, now time.Time) { + s.mu.Lock() + defer s.mu.Unlock() + r, ok := s.items[key] + if !ok { + return + } + r.SupersededBy, r.SupersededAt, r.SupersededOpen, r.SupersededClosedFirst = 0, time.Time{}, false, false + r.AdoptCopyRedHeads = nil + r.AdoptReopenedAt, r.AdoptReopenedFrom = now, from + r.AdoptCheckedAt = now + s.items[key] = r + s.dirty = true +} + +// AdoptionCopyFailed reports whether the record's current copy is the one +// whose failure reopened the original (hivecommons/hive#15135): the copy is +// not polled again and the row reads "copy failed" until a person acts. +func (r Record) AdoptionCopyFailed() bool { + return r.AdoptedBy > 0 && r.AdoptReopenedFrom == r.AdoptedBy && r.AdoptEscalatedReason == LaneReasonAdoptionCopyFailed +} + +// AdoptionReopenedWithin reports whether the original was reopened for a +// failed copy less than window before now. +func (r Record) AdoptionReopenedWithin(now time.Time, window time.Duration) bool { + return !r.AdoptReopenedAt.IsZero() && now.Sub(r.AdoptReopenedAt) < window +} + +// CloseFirstPollDue reports whether a close-first supersession's copy +// should be polled: the original is closed in its favour, the copy has not +// merged, and the last poll is older than interval. +func (r Record) CloseFirstPollDue(now time.Time, interval time.Duration) bool { + if !r.SupersededClosedFirst || r.AdoptedBy <= 0 || r.SupersededBy != r.AdoptedBy { + return false + } + return r.AdoptCheckedAt.IsZero() || now.Sub(r.AdoptCheckedAt) >= interval +} + // SupersederPollDue reports whether a supersede-mode adoption with a // superseding PR on record should be polled: not yet superseded, and the // last poll older than interval.