diff --git a/changelog.d/fixed-stage-label-suppressed-state.md b/changelog.d/fixed-stage-label-suppressed-state.md new file mode 100644 index 0000000000..52d2cb30ec --- /dev/null +++ b/changelog.d/fixed-stage-label-suppressed-state.md @@ -0,0 +1 @@ +- A PR stage the write-back gate kept off GitHub now drives the Overview bands and the your-turn inbox, and the hive no longer announces a label it did not write. On projectbluefin the gate refused every 072/074/085 label on 24 contributor and fork PRs (opt-in label missing, or the backfill guard) from 10-30 on; the stage engine recorded the stage, but bands and ownership read labels, so a PR parked at 085 showed "blocked" with no reason and was in nobody's inbox, while its note said "Moving to `stage/085-needs-decision` so a person sees it" (projectbluefin/fsdk-containers#491, projectbluefin/utah-packages#812). The snapshot now carries the recorded stage beside GitHub's labels (`suppressed_stage`), band and ownership read it, the inbox reason says the label is not on GitHub, the Activity Monitor row's tooltip names the gate's reason (`labels_suppressed`), and escalation notes say the hive would move the PR and why it did not. diff --git a/src/docs/acmm-policy-matrix.md b/src/docs/acmm-policy-matrix.md index 536a87f4cd..ebb577426d 100644 --- a/src/docs/acmm-policy-matrix.md +++ b/src/docs/acmm-policy-matrix.md @@ -255,6 +255,17 @@ listed only so an agent can file regression issues should carry `backfill: false` (default) additionally keeps every kind off items that predate hive's first sight of, or write access to, the repo. +"Internal tracking" means the stage engine still records the PR's stage +when the gate keeps its `stage_label` write off GitHub: the record carries +`labels_suppressed` and the gate's reason, the Overview bands and the +your-turn inbox read that stage in place of the missing label (a refused 085 +park is still *Parked until a maintainer decides*), the Activity Monitor row +says "Stage label not on GitHub" with the reason, and an escalation note +says the hive *would* move the PR to `stage/085-needs-decision` and why it +did not instead of "Moving to". The backfill guard applies to +`stage_label` like every other kind; with the stage tracked internally an +older PR is still in the inbox, only unlabelled on GitHub. + ## Example config alignment `src/hive.yaml.example` uses the built-in `quality` lane for automated test suites, coverage gates, and regression checks. There is no built-in `tester` lane in the ACMM packs, known-agent defaults, or shipped policy templates; operators who want a separate tester must define it as a custom agent with explicit metadata and a policy template. diff --git a/src/pkg/dashboard/overview_export.go b/src/pkg/dashboard/overview_export.go index d03a18ca42..a31323128d 100644 --- a/src/pkg/dashboard/overview_export.go +++ b/src/pkg/dashboard/overview_export.go @@ -479,8 +479,11 @@ func holdItemIssue(item github.HoldItem) github.Issue { } type frontendPR struct { + // pr is the PR as the hive tracks it (withSuppressedStage); wire is the + // snapshot entry as published, with GitHub's labels. pr github.PullRequest verdict *github.MergeVerdict + wire FrontendPR } func frontendRepoPRs(repo FrontendRepo, held bool) []frontendPR { @@ -500,19 +503,19 @@ func frontendRepoPRs(repo FrontendRepo, held bool) []frontendPR { func frontendPullRequest(raw any) (frontendPR, bool) { switch v := raw.(type) { case FrontendPR: - return frontendPR{pr: v.PullRequest, verdict: v.MergeVerdict}, true + return frontendPR{pr: withSuppressedStage(v.PullRequest, v.SuppressedStage), verdict: v.MergeVerdict, wire: v}, true case *FrontendPR: if v == nil { return frontendPR{}, false } - return frontendPR{pr: v.PullRequest, verdict: v.MergeVerdict}, true + return frontendPR{pr: withSuppressedStage(v.PullRequest, v.SuppressedStage), verdict: v.MergeVerdict, wire: *v}, true case github.PullRequest: - return frontendPR{pr: v}, true + return frontendPR{pr: v, wire: FrontendPR{PullRequest: v}}, true case *github.PullRequest: if v == nil { return frontendPR{}, false } - return frontendPR{pr: *v}, true + return frontendPR{pr: *v, wire: FrontendPR{PullRequest: *v}}, true default: return frontendPR{}, false } diff --git a/src/pkg/dashboard/overview_status.go b/src/pkg/dashboard/overview_status.go index d19a699093..177c769b64 100644 --- a/src/pkg/dashboard/overview_status.go +++ b/src/pkg/dashboard/overview_status.go @@ -217,7 +217,7 @@ func (s *Server) statusWithOverviewBands(status *StatusPayload, now time.Time) * slice := overviewPRChartSlice(entry.pr, entry.verdict, ownership, autoMergeLabel) term := overviewPRTerm(ownership, held || len(holdLabels(entry.pr.Labels, status.HiveID)) > 0, entry.pr, entry.verdict, autoMergeLabel) countSlice(slices.PRs, slice) - result = append(result, overviewStatusPR{FrontendPR{entry.pr, entry.verdict}, info, ownership, slice, term, criticalPathFor(paths, ownershipKindPR, entry.pr.Number)}) + result = append(result, overviewStatusPR{entry.wire, info, ownership, slice, term, criticalPathFor(paths, ownershipKindPR, entry.pr.Number)}) } return result } diff --git a/src/pkg/dashboard/ownership.go b/src/pkg/dashboard/ownership.go index bde6f7fcc8..49382da771 100644 --- a/src/pkg/dashboard/ownership.go +++ b/src/pkg/dashboard/ownership.go @@ -923,7 +923,7 @@ func decideRepoOwnership(repos []FrontendRepo, cfg config.DashboardIssueBandsCon } pr := entry.pr pr.Repo = nonEmpty(pr.Repo, repoName) - record(ownershipKindPR, pr.Number, pr.URL, decidePROwnershipGated(pr, entry.verdict, held, cfg, env), pr.CreatedAt, pr.UpdatedAt) + record(ownershipKindPR, pr.Number, pr.URL, withSuppressedStageNote(decidePROwnershipGated(pr, entry.verdict, held, cfg, env), entry.wire.SuppressedStage), pr.CreatedAt, pr.UpdatedAt) } } prRows(repo.OpenPrs, false) diff --git a/src/pkg/dashboard/pr_suppressed_stage.go b/src/pkg/dashboard/pr_suppressed_stage.go new file mode 100644 index 0000000000..9814a1c0a1 --- /dev/null +++ b/src/pkg/dashboard/pr_suppressed_stage.go @@ -0,0 +1,78 @@ +package dashboard + +import ( + "strings" + + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Stage labels the write-back gate kept off a PR (hivecommons/hive#15122 +// containment). The stage engine still records the stage, but the bands and +// the your-turn decision read labels, so a contributor PR the hive parked at +// 085 showed as "blocked" with no reason and was in nobody's inbox +// (projectbluefin, 2026-10-30 → 11-05: 24 PRs, among them +// projectbluefin/fsdk-containers#491 and projectbluefin/utah-packages#812). +// The snapshot carries the recorded stage beside the real labels, and every +// reader that decodes a PR (frontendPullRequest) sees it in place of the +// stage labels GitHub has. + +// attachSuppressedStages stamps each snapshot PR whose last stage write the +// gate refused with the stage its record holds. +func attachSuppressedStages(repos []FrontendRepo, store *stagelabels.Store) { + if store == nil { + return + } + for ri := range repos { + repoName := overviewRepoName(repos[ri]) + stamp := func(items []any) { + for i, raw := range items { + fp, ok := raw.(FrontendPR) + if !ok { + continue + } + fp.SuppressedStage, fp.SuppressedStageReason = suppressedStageFor(store, nonEmpty(fp.Repo, repoName), fp.Number) + items[i] = fp + } + } + stamp(repos[ri].OpenPrs) + stamp(repos[ri].HeldPrs) + } +} + +// suppressedStageFor is the PR stage label the record holds while the gate +// keeps labels off the item, with the gate's reason; "" otherwise. +func suppressedStageFor(store *stagelabels.Store, repo string, number int) (string, string) { + rec, ok := store.Get(stagelabels.Key(repo, number)) + if !ok || !rec.LabelsSuppressed || !rec.Stage.Applies(stagelabels.PRItem) { + return "", "" + } + return rec.Stage.Label(), rec.LabelsSuppressedReason +} + +// withSuppressedStage is pr as the hive tracks it: its stage/* labels +// replaced by stage, the one it could not write. Labels are copied; the +// snapshot keeps what GitHub shows. +func withSuppressedStage(pr github.PullRequest, stage string) github.PullRequest { + if stage == "" { + return pr + } + labels := make([]string, 0, len(pr.Labels)+1) + for _, l := range pr.Labels { + if !strings.HasPrefix(strings.ToLower(strings.TrimSpace(l)), "stage/") { + labels = append(labels, l) + } + } + pr.Labels = append(labels, stage) + return pr +} + +// withSuppressedStageNote tells the person a your-turn PR was put in front +// of that GitHub does not show the stage: they will not find the label. +func withSuppressedStageNote(o Ownership, stage string) Ownership { + if stage == "" || !o.YourTurn { + return o + } + o.Reason += " (the hive holds it at " + stage + "; the write-back gate kept the label off GitHub)" + return o +} diff --git a/src/pkg/dashboard/pr_suppressed_stage_test.go b/src/pkg/dashboard/pr_suppressed_stage_test.go new file mode 100644 index 0000000000..a9aa8fc313 --- /dev/null +++ b/src/pkg/dashboard/pr_suppressed_stage_test.go @@ -0,0 +1,100 @@ +package dashboard + +import ( + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// projectbluefin, 10-30 → 11-05: the write-back gate kept every 072/074/085 +// label off 24 contributor and fork PRs while the stage engine recorded the +// stage. Bands and your-turn read labels, so a PR parked at 085 showed +// "blocked" with no reason and your_turn=false +// (projectbluefin/fsdk-containers#491, projectbluefin/utah-packages#812, +// which still wore a stale stage/074). The record now stands in for the +// label it could not write: band and ownership match the written label. +func TestSuppressedStageDrivesBandAndOwnership(t *testing.T) { + now := ownershipNow11565 + mk := func(labels ...string) FrontendPR { + return FrontendPR{PullRequest: github.PullRequest{ + Repo: "projectbluefin/utah-packages", Number: 812, Title: "fix: pin", Author: "arteeh", + Labels: labels, Mergeable: github.MergeableNo, CreatedAt: now.Add(-6 * 24 * time.Hour), UpdatedAt: now.Add(-time.Hour), + }} + } + decide := func(fp FrontendPR) (Ownership, string) { + repos := []FrontendRepo{{Name: "utah-packages", Full: "projectbluefin/utah-packages", OpenPrs: []any{fp}}} + decideRepoOwnership(repos, config.DashboardIssueBandsConfig{}, ownershipEnv11565(5)) + o := repos[0].Ownership[ownershipKey(ownershipKindPR, 812)] + entry, _ := frontendPullRequest(repos[0].OpenPrs[0]) + return o, prBand(entry.pr, entry.verdict, false, config.DashboardIssueBandsConfig{}, now, github.AutoMergeQueuedLabel, "").Band + } + + written, writtenBand := decide(mk("role/trusted", stagelabels.NeedsDecision.Label())) + if !written.YourTurn || written.ReasonCode != OwnershipReasonParkedNeedsDecision { + t.Fatalf("fixture: written 085 = %+v, want your turn, parked_needs_decision", written) + } + + store := stagelabels.NewStore("", func() time.Time { return now }) + key := stagelabels.Key("projectbluefin/utah-packages", 812) + store.Observe(key, stagelabels.NeedsDecision, "adoption-stalled:reviewer-verdict:changes_requested") + store.NoteLabelsSuppressed(key, true, "needs-opt-in-label — #812 carries none of hive-review") + repos := []FrontendRepo{{Name: "utah-packages", Full: "projectbluefin/utah-packages", OpenPrs: []any{mk("role/trusted", stagelabels.AdoptedByHive.Label())}}} + attachSuppressedStages(repos, store) + fp := repos[0].OpenPrs[0].(FrontendPR) + if fp.SuppressedStage != stagelabels.NeedsDecision.Label() || !strings.Contains(fp.SuppressedStageReason, "needs-opt-in-label") { + t.Fatalf("attached = %q / %q, want the recorded 085 and the gate's reason", fp.SuppressedStage, fp.SuppressedStageReason) + } + if len(fp.Labels) != 2 || fp.Labels[1] != stagelabels.AdoptedByHive.Label() { + t.Fatalf("the snapshot keeps GitHub's labels: %v", fp.Labels) + } + + got, gotBand := decide(fp) + if !got.YourTurn || got.ReasonCode != written.ReasonCode || got.Verb != written.Verb { + t.Fatalf("suppressed 085 = %+v, want the written label's decision %+v", got, written) + } + if !strings.HasPrefix(got.Reason, written.Reason) || !strings.Contains(got.Reason, "kept the label off GitHub") { + t.Fatalf("reason = %q, want %q plus why GitHub shows no label", got.Reason, written.Reason) + } + if gotBand != writtenBand || gotBand != "waiting" { + t.Fatalf("band = %q, written label gives %q; want both waiting", gotBand, writtenBand) + } + + // The same record with its write landed (labels carry the stage) or no + // record at all: nothing is attached, the labels decide as before. + store.NoteLabelsSuppressed(key, false, "") + repos[0].OpenPrs[0] = mk("role/trusted", stagelabels.AdoptedByHive.Label()) + attachSuppressedStages(repos, store) + if fp := repos[0].OpenPrs[0].(FrontendPR); fp.SuppressedStage != "" { + t.Fatalf("landed write still attached %q", fp.SuppressedStage) + } + if o, _ := decide(repos[0].OpenPrs[0].(FrontendPR)); o.ReasonCode == OwnershipReasonParkedNeedsDecision { + t.Fatalf("no suppression: the stale record must not park the PR: %+v", o) + } +} + +func TestStageTrackerRowNamesSuppressedLabels(t *testing.T) { + now := time.Date(2026, 10, 29, 20, 30, 0, 0, time.UTC) + s, store, status := offlineClankerFixture(t, now, now.Add(-3*time.Hour)) + key := stagelabels.Key("hivecommons/hive", 15312) + store.Observe(key, stagelabels.NeedsDecision, "contributor-lane:decision-requested") + store.NoteLabelsSuppressed(key, true, "needs-opt-in-label — #15312 carries none of hive-review") + if it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 15312); !strings.HasPrefix(it.LabelsSuppressed, "needs-opt-in-label") { + t.Fatalf("row labels_suppressed = %q, want the gate's reason", it.LabelsSuppressed) + } + store.NoteLabelsSuppressed(key, false, "") + if it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 15312); it.LabelsSuppressed != "" { + t.Fatalf("landed write: row labels_suppressed = %q, want empty", it.LabelsSuppressed) + } + + html := indexHTML(t) + runStageTrackerRowActionsScript(t, "const assert = require('assert');\n"+jsFunc(t, html, "stageTrackerLabelsSuppressedText")+` +assert.strictEqual(stageTrackerLabelsSuppressedText({}), ''); +const tip = stageTrackerLabelsSuppressedText({ labels_suppressed: 'needs-opt-in-label — #812 carries none of hive-review' }); +assert.ok(tip.startsWith('Stage label not on GitHub'), tip); +assert.ok(tip.includes('needs-opt-in-label'), tip); +`) +} diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index 940b9f8c14..b4ca7a4357 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -2917,6 +2917,7 @@ func (s *Server) UpdateStatusIfFresh(status *StatusPayload, buildEpoch uint64) b status.RepoTargetIssue = "" } } + attachSuppressedStages(status.Repos, s.stageStoreOrShared()) status.ContributorPool = s.BuildContributorPoolStatus() status.Inception = s.buildFrontendInception() if s.contributeHub != nil { diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index a75ff9d466..e9e951d51a 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -574,6 +574,10 @@ type StageTrackerItem struct { // label on its next pass, hivecommons/hive#14586). Rendered as a muted // operator tooltip, never as the stage word. StaleLabels []string `json:"stale_labels,omitempty"` + // LabelsSuppressed is the write-back gate's reason when it kept this + // row's stage label off GitHub: the stage shown is the hive's record, + // not a label on the item. "" when the labels carry the stage. + LabelsSuppressed string `json:"labels_suppressed,omitempty"` // MergeTrain is set on a PR riding a conflict-aware merge train // (hivecommons/hive#15117): its position, the front, the PRs ahead and // the overlapping files. See stage_tracker_merge_train.go. diff --git a/src/pkg/dashboard/stage_tracker_build.go b/src/pkg/dashboard/stage_tracker_build.go index 840f08daab..74d94ba3a0 100644 --- a/src/pkg/dashboard/stage_tracker_build.go +++ b/src/pkg/dashboard/stage_tracker_build.go @@ -140,6 +140,9 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto it.DCOTotal, it.DCOUnsigned = total, unsigned } } + if hasRec && rec.LabelsSuppressed && rec.Stage != stagelabels.None { + it.LabelsSuppressed = nonEmpty(rec.LabelsSuppressedReason, "write-back gate") + } it.Hold = stageTrackerHoldFrom(rec, hasRec, labels) var attempt github.MergeAttempt var hasAttempt bool diff --git a/src/pkg/dashboard/stage_tracker_offline_clanker_test.go b/src/pkg/dashboard/stage_tracker_offline_clanker_test.go index 7856451eae..7a02b4a84b 100644 --- a/src/pkg/dashboard/stage_tracker_offline_clanker_test.go +++ b/src/pkg/dashboard/stage_tracker_offline_clanker_test.go @@ -51,7 +51,7 @@ func TestStageTrackerSuppressedDecisionRecordKeepsStage(t *testing.T) { s, store, status := offlineClankerFixture(t, now, now.Add(-3*time.Hour)) key := stagelabels.Key("hivecommons/hive", 15312) store.Observe(key, stagelabels.NeedsDecision, "contributor-lane:decision-requested") - store.NoteLabelsSuppressed(key, true) + store.NoteLabelsSuppressed(key, true, "needs-opt-in-label") it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 15312) if it.Stage != stagelabels.NeedsDecision.Number() || it.StageSince == nil { t.Fatalf("suppressed 085 record: stage %d since %v caption %q, want 085 from the record", it.Stage, it.StageSince, it.Caption) @@ -61,7 +61,7 @@ func TestStageTrackerSuppressedDecisionRecordKeepsStage(t *testing.T) { } // The same record with the label write landed and then removed is a // lifted park, as before. - store.NoteLabelsSuppressed(key, false) + store.NoteLabelsSuppressed(key, false, "") if it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 15312); it.Stage == stagelabels.NeedsDecision.Number() { t.Fatalf("label-less 085 record with labels not suppressed still parks the row: %+v", it) } diff --git a/src/pkg/dashboard/static-src/js/tracker-actions.js b/src/pkg/dashboard/static-src/js/tracker-actions.js index 9dcb6f83e6..4f532ebfdc 100644 --- a/src/pkg/dashboard/static-src/js/tracker-actions.js +++ b/src/pkg/dashboard/static-src/js/tracker-actions.js @@ -292,6 +292,14 @@ if (!stale.length) return ''; return 'Stale label' + (stale.length > 1 ? 's' : '') + ': ' + stale.join(', ') + ' (stage has moved on; the hive removes it on its next pass)'; } + // Pure: the operator note for a stage the write-back gate kept off + // GitHub (labels_suppressed: the gate's reason). The row's stage is the + // hive's record; the item carries no such label. '' when none. + function stageTrackerLabelsSuppressedText(item) { + const why = item && typeof item.labels_suppressed === 'string' ? item.labels_suppressed.trim() : ''; + if (!why) return ''; + return 'Stage label not on GitHub: the write-back gate refused it (' + why + '). The hive tracks this stage itself.'; + } // Pure: whether a row is an issue waiting on a maintainer's triage. function stageTrackerTriageNeeded(item) { if (!item || item.kind === 'pr') return false; diff --git a/src/pkg/dashboard/static-src/js/tracker-row-strip.js b/src/pkg/dashboard/static-src/js/tracker-row-strip.js index fd757a4345..887b3ff2ee 100644 --- a/src/pkg/dashboard/static-src/js/tracker-row-strip.js +++ b/src/pkg/dashboard/static-src/js/tracker-row-strip.js @@ -723,6 +723,8 @@ if (mods.length) lines.push('Flags: ' + mods.map(m => STAGE_TRACKER_MOD_TEXT[m] || m).join(', ')); const stale = stageTrackerStaleLabelsText(item); if (stale) lines.push(stale); + const suppressed = typeof stageTrackerLabelsSuppressedText === 'function' ? stageTrackerLabelsSuppressedText(item) : ''; + if (suppressed) lines.push(suppressed); const deferred = stageTrackerDeferredMods(item); if (deferred.length) lines.push(STAGE_TRACKER_DEFERRED_NOTE + deferred.map(m => STAGE_TRACKER_MOD_TEXT[m] || m).join(', ')); if (item.sentinel_alert && deferred.indexOf(STAGE_TRACKER_SENTINEL_CHIP) < 0) lines.push(stageTrackerSentinelTip(item)); diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index f6e2eb9b2c..519348a3e1 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -19110,6 +19110,8 @@

Where to get help

if (mods.length) lines.push('Flags: ' + mods.map(m => STAGE_TRACKER_MOD_TEXT[m] || m).join(', ')); const stale = stageTrackerStaleLabelsText(item); if (stale) lines.push(stale); + const suppressed = typeof stageTrackerLabelsSuppressedText === 'function' ? stageTrackerLabelsSuppressedText(item) : ''; + if (suppressed) lines.push(suppressed); const deferred = stageTrackerDeferredMods(item); if (deferred.length) lines.push(STAGE_TRACKER_DEFERRED_NOTE + deferred.map(m => STAGE_TRACKER_MOD_TEXT[m] || m).join(', ')); if (item.sentinel_alert && deferred.indexOf(STAGE_TRACKER_SENTINEL_CHIP) < 0) lines.push(stageTrackerSentinelTip(item)); @@ -23110,6 +23112,14 @@

All labels

if (!stale.length) return ''; return 'Stale label' + (stale.length > 1 ? 's' : '') + ': ' + stale.join(', ') + ' (stage has moved on; the hive removes it on its next pass)'; } + // Pure: the operator note for a stage the write-back gate kept off + // GitHub (labels_suppressed: the gate's reason). The row's stage is the + // hive's record; the item carries no such label. '' when none. + function stageTrackerLabelsSuppressedText(item) { + const why = item && typeof item.labels_suppressed === 'string' ? item.labels_suppressed.trim() : ''; + if (!why) return ''; + return 'Stage label not on GitHub: the write-back gate refused it (' + why + '). The hive tracks this stage itself.'; + } // Pure: whether a row is an issue waiting on a maintainer's triage. function stageTrackerTriageNeeded(item) { if (!item || item.kind === 'pr') return false; diff --git a/src/pkg/dashboard/status_builder.go b/src/pkg/dashboard/status_builder.go index 11b0ffde76..6611a3f9c1 100644 --- a/src/pkg/dashboard/status_builder.go +++ b/src/pkg/dashboard/status_builder.go @@ -1823,6 +1823,11 @@ func openPRIsMergeable(pr any) bool { type FrontendPR struct { github.PullRequest MergeVerdict *github.MergeVerdict `json:"merge_verdict,omitempty"` + // SuppressedStage is the stage label the hive holds this PR at but the + // write-back gate kept off GitHub, with the gate's reason + // (attachSuppressedStages); "" when the labels carry the stage. + SuppressedStage string `json:"suppressed_stage,omitempty"` + SuppressedStageReason string `json:"suppressed_stage_reason,omitempty"` } // AttachMergeVerdicts stamps the governor's per-PR merge verdicts onto the diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index faaf2b3b18..da9b2251bb 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -17,6 +17,7 @@ import ( "github.com/hivecommons/hive/pkg/effects" hgithub "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/stagelabels" + "github.com/hivecommons/hive/pkg/writeback" ) // Stage-074 adoption (hivecommons/hive#12436). @@ -2104,6 +2105,7 @@ type adoptionEscape struct { // reason) first; failed is the warning logged on an error. Returns the // comments posted. func (c *Engine) postAdoptionEscalationNote(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, reason, body, failed string) (int, error) { + body = c.withStageMoveFact(obs, body) if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { c.warn(failed, "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "reason", reason, "error", err) return 0, err @@ -2112,6 +2114,33 @@ func (c *Engine) postAdoptionEscalationNote(ctx context.Context, store *stagelab return 1, nil } +// withStageMoveFact keeps an escalation note honest about the 085 label. +// The write-back gate keeps hive stage labels off a contributor PR nobody +// opted in (or one older than the hive's first write), so "Moving to +// `stage/085-needs-decision`" announced a label that never landed +// (projectbluefin/fsdk-containers#491, projectbluefin/utah-packages#812). +// The gate is asked without recording a refusal: the stage writer records +// its own when it tries. +func (c *Engine) withStageMoveFact(obs stageObservation, body string) string { + d := c.writebackGate().Decide(obs.Owner+"/"+obs.Repo, writebackItemForObservation(obs), writeback.KindStageLabel) + if d.Allowed { + return body + } + return suppressedStageMove(body, stagelabels.NeedsDecision.Label(), d) +} + +// suppressedStageMove rewrites "Moving to `label`" for a label the gate +// refuses: the hive would move it, says why it did not, and where the stage +// lives instead. +func suppressedStageMove(body, label string, d writeback.Decision) string { + why := d.Reason + if d.Detail != "" { + why += " — " + d.Detail + } + return strings.Replace(body, "Moving to `"+label+"`", + "The hive would move this to `"+label+"`, but the write-back gate keeps its labels off this PR ("+why+"), so no label changes here. The hive tracks it at `"+label+"` and lists it on the dashboard", 1) +} + // adoptionCIPending reports whether the lane saw CI still running on the // head: a fix may be in flight, so the head is not stale yet. func adoptionCIPending(obs stageObservation) bool { diff --git a/src/pkg/github/automerge/automerge_stage_move_suppressed_test.go b/src/pkg/github/automerge/automerge_stage_move_suppressed_test.go new file mode 100644 index 0000000000..3eaafac4a8 --- /dev/null +++ b/src/pkg/github/automerge/automerge_stage_move_suppressed_test.go @@ -0,0 +1,97 @@ +package automerge + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + + "github.com/hivecommons/hive/pkg/stagelabels" + "github.com/hivecommons/hive/pkg/writeback" +) + +// gatedTransport is stubTransport with the process write-back gate. +type gatedTransport struct { + stubTransport + gate *writeback.Gate +} + +func (t gatedTransport) WritebackGate() *writeback.Gate { return t.gate } + +// projectbluefin/fsdk-containers#491 (a fork PR nobody opted in): the hive +// posted "Moving to `stage/085-needs-decision` so a person sees it" twice +// while the write-back gate kept every stage label off the PR. Each +// escalation note now says the hive would move it, why it did not, and +// where the stage is tracked; an opted-in PR keeps the plain sentence. +func TestEscalationNoteSaysWhenTheStageLabelIsRefused(t *testing.T) { + gate := writeback.New(writeback.Config{}, writeback.Deps{LevelFor: func(string) int { return 5 }}, "") + e := &Engine{transport: gatedTransport{gate: gate}} + obs := stageObservation{Display: "projectbluefin/fsdk-containers", Owner: "projectbluefin", Repo: "fsdk-containers", Number: 491, HeadSHA: "c0ad5df0927de888c373ccf9b5caead751ef9a6f", Author: "kylerankin", HeadRepo: "kylerankin/fsdk-containers", OpenedAt: time.Now().Add(time.Hour)} + label := stagelabels.NeedsDecision.Label() + notes := map[string]string{ + "adoption stalled": AdoptionStalledComment(obs, stagelabels.Record{}, "adoption-stalled:reviewer-verdict:changes_requested", 12*time.Hour), + "workflow refused": AdoptionWorkflowRefusedComment(obs), + "fork not adopted": ForkNotAdoptedComment(obs, "adoption-stalled:fork-rehome-refused", 30*time.Hour), + } + for name, body := range notes { + t.Run(name, func(t *testing.T) { + if !strings.Contains(body, "Moving to `"+label+"`") { + t.Fatalf("fixture no longer announces the move:\n%s", body) + } + got := e.withStageMoveFact(obs, body) + if strings.Contains(got, "Moving to `") { + t.Fatalf("a refused label write is still announced as a move:\n%s", got) + } + for _, want := range []string{"would move this to `" + label + "`", "write-back gate", string(writeback.ReasonNeedsLabel), "tracks it at `" + label + "`"} { + if !strings.Contains(got, want) { + t.Fatalf("note lacks %q:\n%s", want, got) + } + } + opted := obs + opted.Labels = []string{writeback.DefaultOptInLabel} + if got := e.withStageMoveFact(opted, body); got != body { + t.Fatalf("an opted-in PR's note must be unchanged:\n%s", got) + } + }) + } + if n := len(gate.Recent()); n != 0 { + t.Fatalf("describing the move recorded %d suppression(s); the stage writer records its own", n) + } + // No gate installed: every write is allowed, the note is unchanged. + if got := (&Engine{transport: stubTransport{}}).withStageMoveFact(obs, notes["workflow refused"]); got != notes["workflow refused"] { + t.Fatalf("no gate: note changed:\n%s", got) + } +} + +// The note the hive actually posts goes through the same check. +func TestPostedEscalationNoteSaysWhenTheStageLabelIsRefused(t *testing.T) { + var posted string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + posted = body.Body + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{}`)) + })) + defer srv.Close() + client := gh.NewClient(nil) + client.BaseURL, _ = url.Parse(srv.URL + "/") + gate := writeback.New(writeback.Config{}, writeback.Deps{LevelFor: func(string) int { return 5 }}, "") + e := &Engine{transport: gatedTransport{gate: gate}, gh: client} + store := stagelabels.NewStore("", time.Now) + obs := stageObservation{Display: "projectbluefin/fsdk-containers", Owner: "projectbluefin", Repo: "fsdk-containers", Number: 491, HeadSHA: "c0ad5df", Author: "kylerankin", OpenedAt: time.Now().Add(time.Hour)} + if n, err := e.postAdoptionEscalationNote(context.Background(), store, stagelabels.Key("projectbluefin/fsdk-containers", 491), obs, adoptionWorkflowRefusedReason, AdoptionWorkflowRefusedComment(obs), "failed"); n != 1 || err != nil { + t.Fatalf("post = (%d, %v)", n, err) + } + if strings.Contains(posted, "Moving to `") || !strings.Contains(posted, "would move this to `"+stagelabels.NeedsDecision.Label()+"`") { + t.Fatalf("posted note announces a refused move:\n%s", posted) + } +} diff --git a/src/pkg/github/stage_labeler.go b/src/pkg/github/stage_labeler.go index edbc54288a..131fb73ac8 100644 --- a/src/pkg/github/stage_labeler.go +++ b/src/pkg/github/stage_labeler.go @@ -65,6 +65,9 @@ func (s *stageLabeler) writeAllowed(owner, repo string, number int, preview stri s.mu.Lock() s.suppressed = true s.reason = d.Reason + if d.Detail != "" { + s.reason += " — " + d.Detail + } s.mu.Unlock() } return d.Allowed diff --git a/src/pkg/github/stage_writer.go b/src/pkg/github/stage_writer.go index c31a90661f..94f9f8ae3e 100644 --- a/src/pkg/github/stage_writer.go +++ b/src/pkg/github/stage_writer.go @@ -39,6 +39,7 @@ func (w *stageWriter) Apply(ctx context.Context, ref stageengine.Ref, hint []str // before calling here), nothing landed on GitHub. out.Added, out.Removed = nil, nil out.Suppressed = true + out.SuppressedReason = l.reason } return out, err } diff --git a/src/pkg/stageengine/reconciler.go b/src/pkg/stageengine/reconciler.go index e13470d4c7..a62ff0334b 100644 --- a/src/pkg/stageengine/reconciler.go +++ b/src/pkg/stageengine/reconciler.go @@ -162,7 +162,7 @@ func (r *reconciler) Reconcile(ctx context.Context, f Facts) (Transition, error) // The gate's refusal is a fact about the record: readers that // compare the record with the item's labels must know the labels // never carried the stage. - r.opts.Store.NoteLabelsSuppressed(key, out.Suppressed) + r.opts.Store.NoteLabelsSuppressed(key, out.Suppressed, out.SuppressedReason) } r.opts.Metrics.WriterWrite(f.Ref.Kind, OpAdd, len(out.Added)) r.opts.Metrics.WriterWrite(f.Ref.Kind, OpRemove, len(out.Removed)) diff --git a/src/pkg/stageengine/reconciler_test.go b/src/pkg/stageengine/reconciler_test.go index a7234ffc75..c076a9959b 100644 --- a/src/pkg/stageengine/reconciler_test.go +++ b/src/pkg/stageengine/reconciler_test.go @@ -419,7 +419,11 @@ type suppressingWriter struct { } func (w *suppressingWriter) Apply(context.Context, Ref, []string, stagelabels.Desired) (stagelabels.Outcome, error) { - return stagelabels.Outcome{Suppressed: w.suppress}, w.err + out := stagelabels.Outcome{Suppressed: w.suppress} + if w.suppress { + out.SuppressedReason = "needs-opt-in-label — #15312 carries none of hive-review" + } + return out, w.err } // TestReconcileRecordsSuppressedLabels: a write the gate kept off GitHub @@ -434,8 +438,8 @@ func TestReconcileRecordsSuppressedLabels(t *testing.T) { t.Fatal(err) } rec, ok := store.Get(f.Ref.Key()) - if !ok || rec.Stage == stagelabels.None || !rec.LabelsSuppressed { - t.Fatalf("suppressed write: record = %+v ok=%t, want stage kept with labels_suppressed", rec, ok) + if !ok || rec.Stage == stagelabels.None || !rec.LabelsSuppressed || rec.LabelsSuppressedReason != "needs-opt-in-label — #15312 carries none of hive-review" { + t.Fatalf("suppressed write: record = %+v ok=%t, want stage kept with labels_suppressed and the gate's reason", rec, ok) } w.err = errors.New("boom") w.suppress = false @@ -449,7 +453,7 @@ func TestReconcileRecordsSuppressedLabels(t *testing.T) { if _, err := r.Reconcile(ctx, f); err != nil { t.Fatal(err) } - if rec, _ := store.Get(f.Ref.Key()); rec.LabelsSuppressed { + if rec, _ := store.Get(f.Ref.Key()); rec.LabelsSuppressed || rec.LabelsSuppressedReason != "" { t.Fatalf("landed write left labels_suppressed set: %+v", rec) } } diff --git a/src/pkg/stagelabels/enforce.go b/src/pkg/stagelabels/enforce.go index a5a3133573..334664e148 100644 --- a/src/pkg/stagelabels/enforce.go +++ b/src/pkg/stagelabels/enforce.go @@ -44,7 +44,10 @@ type Outcome struct { // Suppressed is set when the write-back gate kept the planned labels // off the item (a contributor PR nobody opted in, below the level // stage labels need): the stage is tracked internally only. - Suppressed bool + // SuppressedReason is the gate's reason, for the record and the + // dashboard. + Suppressed bool + SuppressedReason string } // Changed reports whether any label was written. diff --git a/src/pkg/stagelabels/store.go b/src/pkg/stagelabels/store.go index 4b6ed68b08..eed65d68a4 100644 --- a/src/pkg/stagelabels/store.go +++ b/src/pkg/stagelabels/store.go @@ -405,6 +405,9 @@ type Record struct { // is not evidence that a park lifted (hivecommons/hive#15541 rows read // "PR in progress" while the record said 085). LabelsSuppressed bool `json:"labels_suppressed,omitempty"` + // LabelsSuppressedReason is the write-back gate's reason for the last + // refusal ("needs-opt-in-label — #N carries none of …"), "" otherwise. + LabelsSuppressedReason string `json:"labels_suppressed_reason,omitempty"` // TrainEjectedWhy / TrainEjectedHead / TrainEjectedAt record the merge // train's last ejection of this PR (hivecommons/hive#15825): the head // it was ejected at, why (head-moved, ci-pending, ci-red, @@ -2714,16 +2717,20 @@ func HiveHeld(r Record, overrides map[string]time.Duration, now time.Time) bool } // NoteLabelsSuppressed records whether the last stage-label write for key -// was kept off GitHub by the write-back gate. A missing record is left -// missing. -func (s *Store) NoteLabelsSuppressed(key string, suppressed bool) { +// was kept off GitHub by the write-back gate, and the gate's reason. A +// missing record is left missing. +func (s *Store) NoteLabelsSuppressed(key string, suppressed bool, reason string) { s.mu.Lock() defer s.mu.Unlock() r, ok := s.items[key] - if !ok || r.LabelsSuppressed == suppressed { + if !suppressed { + reason = "" + } + if !ok || r.LabelsSuppressed == suppressed && r.LabelsSuppressedReason == reason { return } r.LabelsSuppressed = suppressed + r.LabelsSuppressedReason = reason s.items[key] = r s.dirty = true } diff --git a/src/pkg/stagelabels/store_labels_suppressed_test.go b/src/pkg/stagelabels/store_labels_suppressed_test.go new file mode 100644 index 0000000000..c90cf2c5cd --- /dev/null +++ b/src/pkg/stagelabels/store_labels_suppressed_test.go @@ -0,0 +1,30 @@ +package stagelabels + +import ( + "testing" + "time" +) + +// The gate's reason lives only while the labels are suppressed: a landed +// write clears it, whatever the caller passes. +func TestNoteLabelsSuppressedKeepsReasonOnlyWhileSuppressed(t *testing.T) { + s := NewStore("", time.Now) + key := Key("projectbluefin/utah-packages", 812) + s.Observe(key, NeedsDecision, "adoption-stalled:reviewer-verdict:changes_requested") + s.NoteLabelsSuppressed(key, true, "backfill-off — #812 was created before hive could write") + if r, _ := s.Get(key); !r.LabelsSuppressed || r.LabelsSuppressedReason != "backfill-off — #812 was created before hive could write" { + t.Fatalf("suppressed: %+v", r) + } + s.NoteLabelsSuppressed(key, true, "needs-opt-in-label") + if r, _ := s.Get(key); r.LabelsSuppressedReason != "needs-opt-in-label" { + t.Fatalf("a new reason must replace the old one: %+v", r) + } + s.NoteLabelsSuppressed(key, false, "needs-opt-in-label") + if r, _ := s.Get(key); r.LabelsSuppressed || r.LabelsSuppressedReason != "" { + t.Fatalf("landed: %+v, want flag and reason cleared", r) + } + s.NoteLabelsSuppressed(Key("o/r", 1), true, "x") + if _, ok := s.Get(Key("o/r", 1)); ok { + t.Fatal("a missing record must stay missing") + } +} diff --git a/src/pkg/writeback/gate.go b/src/pkg/writeback/gate.go index 1e83a36737..c283f9dd17 100644 --- a/src/pkg/writeback/gate.go +++ b/src/pkg/writeback/gate.go @@ -396,6 +396,18 @@ func (rec *repoRecord) backfillCutoff() time.Time { // target; kind names the write. A nil gate allows everything (the client // without a gate installed behaves as before) but that is never the case // in cmd/hive, which installs one at boot. +// Decide is what Allowed would answer, without recording a refusal: for a +// writer that describes a write another path makes (the 085 note says +// whether the stage label it announces can land) rather than making it. +func (g *Gate) Decide(repo string, item Item, kind Kind) Decision { + if g == nil { + return Decision{Allowed: true, Reason: ReasonNoGate} + } + g.mu.Lock() + defer g.mu.Unlock() + return g.decideLocked(repo, item, kind) +} + func (g *Gate) Allowed(repo string, item Item, kind Kind) Decision { return g.AllowedPreview(repo, item, kind, "") } diff --git a/src/pkg/writeback/gate_decide_test.go b/src/pkg/writeback/gate_decide_test.go new file mode 100644 index 0000000000..a056d63bf8 --- /dev/null +++ b/src/pkg/writeback/gate_decide_test.go @@ -0,0 +1,34 @@ +package writeback + +import ( + "testing" + "time" +) + +// Decide answers what Allowed would, without the refusal landing in the +// dashboard's suppressed list: a note that describes a stage write another +// writer makes must not count as a second suppression. +func TestDecideAnswersLikeAllowedWithoutRecording(t *testing.T) { + g := New(Config{}, Deps{LevelFor: func(string) int { return 5 }}, "") + pr := Item{Number: 491, IsPR: true, Author: "kylerankin", CreatedAt: time.Now()} + d := g.Decide("o/r", pr, KindStageLabel) + if d.Allowed || d.Reason == "" { + t.Fatalf("Decide on an un-opted contributor PR = %+v, want refused with a reason", d) + } + if n := len(g.Recent()); n != 0 { + t.Fatalf("Decide recorded %d suppression(s), want none", n) + } + if a := g.Allowed("o/r", pr, KindStageLabel); a.Allowed != d.Allowed || a.Reason != d.Reason { + t.Fatalf("Allowed = %+v, Decide = %+v: they must agree", a, d) + } + if n := len(g.Recent()); n != 1 { + t.Fatalf("Allowed recorded %d suppression(s), want 1", n) + } + pr.Labels = []string{DefaultOptInLabel} + if d := g.Decide("o/r", pr, KindStageLabel); !d.Allowed { + t.Fatalf("opted-in PR = %+v, want allowed", d) + } + if d := (*Gate)(nil).Decide("o/r", pr, KindStageLabel); !d.Allowed { + t.Fatalf("nil gate = %+v, want allowed", d) + } +}