From a5b9d89efac4722d596553d302339ad88a0fd20d Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 09:01:18 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20stage=20tracker:=20record=20the?= =?UTF-8?q?=20soak=20deadline=20once=20instead=20of=20re-stamping=20the=20?= =?UTF-8?q?note=20every=20pass?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The contributor lane reported contributor-lane:soaking(remaining=12m0s), a reason that changed every pass; ObserveProgress restamped Reason and LastSeen, dirtying and saving the store each pass, and the dashboard aged the caption from LastSeen while the SPA treated each countdown text change as a new Hive: line. The owner saw the note refresh every minute of a soak. Record the soak head/start/window once on the stage record and report the stable contributor-lane:soaking reason. LastSeen no longer counts as a record change (persisted at most every 10m). The record carries ReasonChangedAt; the dashboard sends note_changed_at and computes the time left from the recorded deadline at request time (soak complete at/after the deadline, never 0s left). The SPA refreshes the countdown in place for the same note. soakRemainingFromReason stays as a legacy parser. Closes #15758 Signed-off-by: Andrew Anderson --- .../fixed-15758-soak-caption-no-restamp.md | 1 + src/docs/hive-merge.md | 8 +- src/pkg/dashboard/stage_tracker.go | 18 ++- src/pkg/dashboard/stage_tracker_build.go | 5 + src/pkg/dashboard/stage_tracker_caption.go | 72 ++++++++--- src/pkg/dashboard/stage_tracker_cues_test.go | 32 +++++ src/pkg/dashboard/stage_tracker_links.go | 1 + .../dashboard/stage_tracker_note_tuck_test.go | 3 + src/pkg/dashboard/stage_tracker_soak_test.go | 78 ++++++++++- src/pkg/dashboard/stage_tracker_test.go | 18 ++- .../dashboard/static-src/js/tracker-motion.js | 40 +++++- src/pkg/dashboard/static/index.html | 40 +++++- .../automerge/automerge_contributor_lane.go | 17 ++- .../automerge_contributor_lane_test.go | 37 ++++-- .../github/automerge/automerge_relay_gate.go | 19 ++- .../automerge/automerge_relay_gate_test.go | 6 +- .../automerge/automerge_stage_labels.go | 3 +- .../github/automerge/automerge_stage_soak.go | 64 ++++++--- .../automerge/automerge_stage_soak_test.go | 101 ++++++++++++--- src/pkg/stagelabels/soak_test.go | 121 ++++++++++++++++++ src/pkg/stagelabels/store.go | 80 +++++++++++- 21 files changed, 651 insertions(+), 113 deletions(-) create mode 100644 changelog.d/fixed-15758-soak-caption-no-restamp.md diff --git a/changelog.d/fixed-15758-soak-caption-no-restamp.md b/changelog.d/fixed-15758-soak-caption-no-restamp.md new file mode 100644 index 0000000000..0ca31162f0 --- /dev/null +++ b/changelog.d/fixed-15758-soak-caption-no-restamp.md @@ -0,0 +1 @@ +- The stage tracker's "Hive:" note for a soaking PR no longer re-stamps itself every eval pass. The contributor lane records the soak head, start and window once on the stage record and reports the stable reason `contributor-lane:soaking`; the dashboard computes the time left from that deadline at request time (showing `soak complete — merges on the next pass` at the deadline instead of `0s left`), sends an explicit `note_changed_at`, and the SPA refreshes the countdown in place rather than opening a new line. `LastSeen` no longer counts as a record change and is persisted at most once per 10 minutes. Records written with the old `contributor-lane:soaking(remaining=…)` reason stay readable (#15758). diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 778d5ed941..647f89cfd3 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -163,7 +163,7 @@ sentinel alert, parked-on-decision), human-merge paths and head-SHA pinning — before `hive-merge` merges it. A refusal is terminal for that request (`.denied`), names the failing gate in the result the agent sees (for example `contributor-lane:claim-tier-untrusted (author tier "untrusted", require_tiers -merger, trusted)` or `contributor-lane:soaking(remaining=27m)`), and is audited +merger, trusted)` or `contributor-lane:soaking, 27m0s remaining`), and is audited as `merge_request_refused`. An agent cannot use the relay to land a contributor PR the lane would not merge; the lane still merges it on its own tick once the gates clear. Hive-authored PRs keep the relay's existing checks. @@ -561,8 +561,10 @@ A PR merges through this tier only when all of the following hold: went green — the latest `completed_at` over its check runs, read once per (repo, PR, head) and then pinned in process — so a pod restart or upgrade does not restart it; a new push restarts - the soak and the skip reason reports the time left - (`contributor-lane:soaking(remaining=25m0s)`). + the soak. The skip reason is the stable `contributor-lane:soaking`; the + soak head, start and window are recorded once on the stage record, so the + stage tracker computes "N left" from that deadline at request time instead + of the hive re-stamping its note every pass. Merges squash through the same effect-claimed `MergePR` path as the other tiers, log `automerge merged tier=contributor-lane admitted_by=`, record the diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 9d2051fc18..abe2885f0b 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -290,6 +290,12 @@ type StageTrackerItem struct { Reason string `json:"reason,omitempty"` Caption string `json:"caption"` Actor string `json:"actor,omitempty"` + // NoteChangedAt is when the caption last changed meaning: the stage + // entry, a new reason class, or a soak that ran out. A caption whose + // text moves while this stands still (a soak countdown) is the same + // note; the SPA refreshes the words in place and ages the note from + // here, not from the sweep that re-read it (hivecommons/hive#15729). + NoteChangedAt *time.Time `json:"note_changed_at,omitempty"` // Modifiers are the health and control markers in effect and drawn as // pills: stalled, regressed, hold, needs-human, needs-decision // (stage/045 on an issue or stage/085 on a PR carried as a park @@ -342,9 +348,10 @@ type StageTrackerItem struct { RegressedAt *time.Time `json:"regressed_at,omitempty"` RegressedReason string `json:"regressed_reason,omitempty"` // SoakRemainingSeconds is, at 095 pre-merge-soak, how much of the lane's - // soak window was left as of the last sweep (the lane's - // `soaking(remaining=…)` reason aged by the time since it was recorded); - // 0 when unknown or not soaking. + // soak window is left now (the record's soak deadline; a record written + // before the deadline existed falls back to its `soaking(remaining=…)` + // reason aged by the time since it was recorded); 0 when unknown or + // not soaking. SoakRemainingSeconds int64 `json:"soak_remaining_seconds,omitempty"` // MergeGate narrates a PR's position at the merge gate (060 / 080 / // 095 / 100): state, what holds it, whether hive's merge sweep is on @@ -620,7 +627,10 @@ type StageTrackerLinkedPR struct { Stage int `json:"stage,omitempty"` StageSince *time.Time `json:"stage_since,omitempty"` Caption string `json:"caption,omitempty"` - Modifiers []string `json:"modifiers,omitempty"` + // NoteChangedAt mirrors the PR row's: when the caption last changed + // meaning. + NoteChangedAt *time.Time `json:"note_changed_at,omitempty"` + Modifiers []string `json:"modifiers,omitempty"` // DeferredModifiers mirrors the nested PR's own row field: the // merge-gate pills it is not yet drawing. DeferredModifiers []string `json:"deferred_modifiers,omitempty"` diff --git a/src/pkg/dashboard/stage_tracker_build.go b/src/pkg/dashboard/stage_tracker_build.go index b4c0d11a35..f1dd6abfa4 100644 --- a/src/pkg/dashboard/stage_tracker_build.go +++ b/src/pkg/dashboard/stage_tracker_build.go @@ -173,6 +173,11 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto if c := stageTrackerSoakCaption(rec, now); c != "" { it.Caption = c } + if hasRec && rec.Stage != stagelabels.None { + if at := stageTrackerNoteChangedAt(rec, now); !at.IsZero() { + it.NoteChangedAt = &at + } + } if hasAttempt && (attempt.InFlight() || attempt.Merged) { it.Caption, it.Actor = stageTrackerMergingCaption(attempt), StageActorHive } diff --git a/src/pkg/dashboard/stage_tracker_caption.go b/src/pkg/dashboard/stage_tracker_caption.go index 87aeaed6ed..1104669fa1 100644 --- a/src/pkg/dashboard/stage_tracker_caption.go +++ b/src/pkg/dashboard/stage_tracker_caption.go @@ -573,11 +573,14 @@ func stageTrackerUntrustedAuthorFix(base, author string) string { return "" } -// trackerSoakRemainingRE pulls the time left out of the lane's -// `contributor-lane:soaking(remaining=12m0s)` reason. +// trackerSoakRemainingRE pulls the time left out of the +// `contributor-lane:soaking(remaining=12m0s)` reason the lane wrote before +// the soak deadline lived on the record (hivecommons/hive#15729). Records +// of that shape may still be on disk; nothing writes it any more. var trackerSoakRemainingRE = regexp.MustCompile(`remaining=([0-9hms.]+)`) -// soakRemainingFromReason parses the soak time left from a soaking reason. +// soakRemainingFromReason parses the soak time left from a soaking reason +// of the pre-deadline shape; false for the bare `contributor-lane:soaking`. func soakRemainingFromReason(reason string) (time.Duration, bool) { m := trackerSoakRemainingRE.FindStringSubmatch(reason) if m == nil { @@ -591,41 +594,72 @@ func soakRemainingFromReason(reason string) (time.Duration, bool) { } // stageTrackerSoakCaption is the 095 sentence for a record carrying the -// lane's soak clock: "soaking — 12m left" while the window runs, aged from -// the lane's last read so a soak that has run out never says "0s left"; -// past it, "soak complete — merges on the next pass". "" when the record -// is not at 095 or carries no clock (the generic caption stands). +// lane's soak clock: "soaking — 12m left" while the window runs, computed +// at request time from the record's deadline so a soak that has run out +// never says "0s left"; past it, "soak complete — merges on the next +// pass". "" when the record is not at 095 or carries no clock (the +// generic caption stands). The words move each minute, but the note's +// age (NoteChangedAt) does not: the SPA refreshes them in place. func stageTrackerSoakCaption(rec stagelabels.Record, now time.Time) string { const admitted = "Admitted by the automerge lane; " if rec.Stage != stagelabels.PreMergeSoak { return "" } - if _, ok := soakRemainingFromReason(rec.Reason); !ok { + until, ok := stageTrackerSoakUntil(rec) + if !ok { return "" } - if left := stageTrackerSoakRemaining(rec, now); left > 0 { + if left := until.Sub(now); left > 0 { return admitted + "soaking — " + shortDuration(left) + " left" } return admitted + "soak complete — merges on the next pass" } -// stageTrackerSoakRemaining is the soak time left now for a record at 095: -// the lane's recorded remaining, aged by the time since the sweep wrote it. -func stageTrackerSoakRemaining(rec stagelabels.Record, now time.Time) time.Duration { +// stageTrackerSoakUntil is the soak deadline a 095 record carries: the +// recorded soak's start plus its window (NoteSoak), else — for a record +// written before the deadline existed — the lane's `soaking(remaining=…)` +// reason aged from the sweep that wrote it. ok is false when the record +// is not at 095 or carries no clock. +func stageTrackerSoakUntil(rec stagelabels.Record) (time.Time, bool) { if rec.Stage != stagelabels.PreMergeSoak { - return 0 + return time.Time{}, false + } + if _, startedAt, window, ok := rec.Soak(); ok { + return startedAt.Add(window), true } left, ok := soakRemainingFromReason(rec.Reason) + if !ok || rec.LastSeen.IsZero() { + return time.Time{}, false + } + return rec.LastSeen.Add(left), true +} + +// stageTrackerSoakRemaining is the soak time left now for a record at 095, +// 0 once the deadline has passed or when the record carries no clock. +func stageTrackerSoakRemaining(rec stagelabels.Record, now time.Time) time.Duration { + until, ok := stageTrackerSoakUntil(rec) if !ok { return 0 } - if !rec.LastSeen.IsZero() { - left -= now.Sub(rec.LastSeen) - } - if left < 0 { - return 0 + if left := until.Sub(now); left > 0 { + return left } - return left + return 0 +} + +// stageTrackerNoteChangedAt is when the row's hive note last changed +// meaning: the record's reason-class change (or stage entry), or — for a +// 095 row whose soak has run out — the deadline, since "soak complete" +// is a different note from "soaking". Never later than now. +func stageTrackerNoteChangedAt(rec stagelabels.Record, now time.Time) time.Time { + at := rec.NoteChangedAt() + if until, ok := stageTrackerSoakUntil(rec); ok && !until.After(now) && until.After(at) { + at = until + } + if at.After(now) { + at = now + } + return at } // adoptedWorkCaption names what the hive is doing on a PR it adopted in diff --git a/src/pkg/dashboard/stage_tracker_cues_test.go b/src/pkg/dashboard/stage_tracker_cues_test.go index 6e02bd7b9b..a4f3893daa 100644 --- a/src/pkg/dashboard/stage_tracker_cues_test.go +++ b/src/pkg/dashboard/stage_tracker_cues_test.go @@ -31,6 +31,9 @@ func stageTrackerCuesJS(t *testing.T, html string) string { jsConstLine(t, html, "STAGE_TRACKER_EVENT_COVERED_BY_PR"), jsFunc(t, html, "stageTrackerEventText"), jsFunc(t, html, "stageTrackerEventsNext"), + jsFunc(t, html, "stageTrackerEventShownAt"), + jsFunc(t, html, "stageTrackerSameNote"), + jsFunc(t, html, "stageTrackerNewEvent"), jsFunc(t, html, "stageTrackerNoteOwnerEvent"), jsFunc(t, html, "stageTrackerEventHtml"), jsFunc(t, html, "stageTrackerLocalMidnight"), @@ -207,6 +210,34 @@ assert.strictEqual(events['hivecommons/hive/pr#14947'].text, 'moved to Needs dec assert.deepStrictEqual(stageTrackerEventsNext({}, null, next, stages, now), {}, 'the first payload never writes a line for everything'); events = stageTrackerEventsNext(events, next, next, stages, now + STAGE_TRACKER_EVENT_MS + 1); assert.deepStrictEqual(events, {}, 'lines expire'); +// A soaking row (hivecommons/hive#15729): the caption counts down each +// poll but note_changed_at stands still, so the line is one note — its +// words refresh in place, its age counts from the server's stamp and the +// line neither re-opens nor is re-written when it has expired. +const soakStages = [{ number: 80, label: 'Merge gate' }, { number: 95, label: 'Pre-merge soak' }]; +const soakAt = new Date(now - 120000).toISOString(); +const soakRow = (caption, noteAt) => ({ repo: 'hivecommons/hive', kind: 'pr', number: 15730, stage: 95, caption, note_changed_at: noteAt }); +const gate = stageTrackerStateMap([Object.assign(soakRow('Awaiting merge admission', new Date(now - 600000).toISOString()), { stage: 80 })]); +const soak10 = stageTrackerStateMap([soakRow('Admitted by the automerge lane; soaking — 10m left', soakAt)]); +const soak9 = stageTrackerStateMap([soakRow('Admitted by the automerge lane; soaking — 9m left', soakAt)]); +const soakKey = 'hivecommons/hive/pr#15730'; +let soakEv = stageTrackerEventsNext({}, gate, soak10, soakStages, now); +assert.strictEqual(soakEv[soakKey].text, 'moved to Pre-merge soak'); +assert.strictEqual(soakEv[soakKey].at, now - 120000, 'the age counts from when the note changed on the server'); +assert.strictEqual(soakEv[soakKey].openAt, now, 'but the 30 s open window starts when this tab first shows it'); +soakEv = stageTrackerEventsNext(soakEv, soak10, soak9, soakStages, now + 60000); +assert.strictEqual(soakEv[soakKey].text, 'Admitted by the automerge lane; soaking — 9m left', 'the words move in place'); +assert.strictEqual(soakEv[soakKey].at, now - 120000, 'the age does not reset'); +assert.strictEqual(soakEv[soakKey].openAt, now, 'the line does not re-open'); +assert.deepStrictEqual(stageTrackerEventsNext({}, soak10, soak9, soakStages, now + STAGE_TRACKER_EVENT_MS * 2), {}, 'an expired line is not re-written by the countdown'); +const soakDone = stageTrackerStateMap([soakRow('Admitted by the automerge lane; soak complete — merges on the next pass', new Date(now + 480000).toISOString())]); +soakEv = stageTrackerEventsNext(soakEv, soak9, soakDone, soakStages, now + 500000); +assert.strictEqual(soakEv[soakKey].text, 'Admitted by the automerge lane; soak complete — merges on the next pass', 'a new note_changed_at is a new note'); +assert.strictEqual(soakEv[soakKey].at, now + 480000); +const legacy9 = stageTrackerStateMap([soakRow('soaking — 9m left', '')]); +const legacy8 = stageTrackerStateMap([soakRow('soaking — 8m left', '')]); +assert.strictEqual(stageTrackerEventsNext({}, legacy9, legacy8, soakStages, now)[soakKey].at, now, 'without the stamp (an older payload) a caption change is a new line as before'); +assert.strictEqual(stageTrackerNewEvent('x', new Date(now + 5000).toISOString(), now).at, now, 'a stamp in the future is clamped to now'); _stageTrackerEvents = {}; stageTrackerNoteOwnerEvent('k', 'approved', now); let line = stageTrackerEventHtml('k', { events: _stageTrackerEvents, nowMs: now }); @@ -329,6 +360,7 @@ func stageTrackerCelebrateJS(t *testing.T, html string) string { jsFunc(t, html, "hamBannerDismiss"), jsFunc(t, html, "hamBannerShowRow"), jsFunc(t, html, "stageTrackerEventLineInject"), + jsFunc(t, html, "stageTrackerEventShownAt"), jsFunc(t, html, "stageTrackerEventHtml"), jsFunc(t, html, "stageTrackerNoteOwnerEvent"), jsFunc(t, html, "stageTrackerCueAgeSync"), diff --git a/src/pkg/dashboard/stage_tracker_links.go b/src/pkg/dashboard/stage_tracker_links.go index 7626fff850..993f253a22 100644 --- a/src/pkg/dashboard/stage_tracker_links.go +++ b/src/pkg/dashboard/stage_tracker_links.go @@ -337,6 +337,7 @@ func stageTrackerResolveLinkedPRs(items []StageTrackerItem, open func(repo strin lp.SoakRemainingSeconds, lp.MergeGate = pr.SoakRemainingSeconds, pr.MergeGate lp.MergeTrain, lp.LeftTrain, lp.Overlap = pr.MergeTrain, pr.LeftTrain, pr.Overlap lp.Soak = pr.Soak + lp.NoteChangedAt = pr.NoteChangedAt lp.LastOperatorAction, lp.OperatorActionCount = pr.LastOperatorAction, pr.OperatorActionCount lp.Decided, lp.AdoptedFrom = pr.Decided, pr.AdoptedFrom lp.AdoptedBy, lp.AdoptedByState = pr.AdoptedBy, pr.AdoptedByState diff --git a/src/pkg/dashboard/stage_tracker_note_tuck_test.go b/src/pkg/dashboard/stage_tracker_note_tuck_test.go index 70158f138d..25d07f2673 100644 --- a/src/pkg/dashboard/stage_tracker_note_tuck_test.go +++ b/src/pkg/dashboard/stage_tracker_note_tuck_test.go @@ -30,6 +30,9 @@ func stageTrackerNoteTuckJS(t *testing.T, html string) string { jsConstLine(t, html, "STAGE_TRACKER_EVENT_COVERED_BY_PR"), jsFunc(t, html, "stageTrackerEventText"), jsFunc(t, html, "stageTrackerEventsNext"), + jsFunc(t, html, "stageTrackerEventShownAt"), + jsFunc(t, html, "stageTrackerSameNote"), + jsFunc(t, html, "stageTrackerNewEvent"), jsFunc(t, html, "stageTrackerNoteEvents"), jsFunc(t, html, "stageTrackerNoteOwnerEvent"), jsFunc(t, html, "stageTrackerEventHtml"), diff --git a/src/pkg/dashboard/stage_tracker_soak_test.go b/src/pkg/dashboard/stage_tracker_soak_test.go index add5d23a0b..b972ba542f 100644 --- a/src/pkg/dashboard/stage_tracker_soak_test.go +++ b/src/pkg/dashboard/stage_tracker_soak_test.go @@ -83,13 +83,17 @@ func TestStageTrackerSoakInPayload(t *testing.T) { s.deps.Config = &config.Config{} s.deps.Config.AutoMerge.ContributorLane.SoakMinutes = 45 - // PR 102 (095) has no NoteSoak yet: the reason fallback with the 45m window. + // A 095 record written before NoteSoak existed (the countdown in its + // reason, no clock): the reason fallback with the 45m window. + store.ClearSoak(stagelabels.Key("acme/console", 102)) + store.Observe(stagelabels.Key("acme/console", 102), stagelabels.PreMergeSoak, "contributor-lane:soaking(remaining=7m0s)") pr := trackerItem(t, s.buildStageTracker(s.status, store, now), "pr", 102) if pr.Soak == nil || pr.Soak.WindowSeconds != 45*60 || !pr.Soak.Until.Equal(now.Add(7*time.Minute)) { t.Fatalf("PR 102 soak from reason: %+v", pr.Soak) } // The pass records the soak: the record's clock wins. started := now.Add(-20 * time.Minute) + store.Observe(stagelabels.Key("acme/console", 102), stagelabels.PreMergeSoak, "contributor-lane:soaking") store.NoteSoak(stagelabels.Key("acme/console", 102), "abc1234def", started, 30*time.Minute) resp := s.buildStageTracker(s.status, store, now) pr = trackerItem(t, resp, "pr", 102) @@ -259,3 +263,75 @@ assert.deepStrictEqual([stageTrackerSoakLeftText(45000), stageTrackerSoakLeftTex console.log('soak clock ok'); `) } + +// TestStageTrackerSoakCaptionFromDeadline (hivecommons/hive#15729): the +// 095 caption counts down from the record's soak deadline at request +// time — a sweep that re-reads the same reason moves nothing — never says +// "0s left", and the note's age is the stage entry while soaking and the +// deadline once the soak is complete. A record written before the +// deadline existed still reads its `soaking(remaining=…)` reason aged +// from the sweep that wrote it. +func TestStageTrackerSoakCaptionFromDeadline(t *testing.T) { + now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC) + entered := now.Add(-40 * time.Minute) + rec := stagelabels.Record{Stage: stagelabels.PreMergeSoak, Reason: "contributor-lane:soaking", EnteredAt: entered, ReasonChangedAt: entered, LastSeen: now.Add(-30 * time.Second), + SoakHead: "abc", SoakStartedAt: now.Add(-20 * time.Minute), SoakWindowSeconds: 1800} + if got := stageTrackerSoakCaption(rec, now); got != "Admitted by the automerge lane; soaking — 10m left" { + t.Fatalf("caption = %q", got) + } + if left := stageTrackerSoakRemaining(rec, now); left != 10*time.Minute { + t.Fatalf("remaining = %v", left) + } + if at := stageTrackerNoteChangedAt(rec, now); !at.Equal(entered) { + t.Fatalf("note changed at %v, want stage entry %v", at, entered) + } + // A later sweep re-reads the reason: LastSeen moves, nothing else does. + later := rec + later.LastSeen = now + if a, b := stageTrackerNoteChangedAt(rec, now), stageTrackerNoteChangedAt(later, now); !a.Equal(b) { + t.Fatalf("a re-read moved the note's age: %v → %v", a, b) + } + if got := stageTrackerSoakCaption(later, now.Add(time.Minute)); got != "Admitted by the automerge lane; soaking — 9m left" { + t.Fatalf("a minute later: %q", got) + } + // At and past the deadline: never "0s left"; the note changed at the deadline. + deadline := rec.SoakStartedAt.Add(30 * time.Minute) + for _, at := range []time.Time{deadline, deadline.Add(90 * time.Second)} { + if got := stageTrackerSoakCaption(rec, at); got != "Admitted by the automerge lane; soak complete — merges on the next pass" { + t.Fatalf("at %v: %q", at, got) + } + if left := stageTrackerSoakRemaining(rec, at); left != 0 { + t.Fatalf("at %v: remaining = %v", at, left) + } + if got := stageTrackerNoteChangedAt(rec, at); !got.Equal(deadline) { + t.Fatalf("at %v: note changed at %v, want the deadline %v", at, got, deadline) + } + } + // A new reason class at the same stage dates the note from then. + held := rec + held.Reason, held.ReasonChangedAt = "release-hold", now.Add(-time.Minute) + if at := stageTrackerNoteChangedAt(held, now); !at.Equal(now.Add(-time.Minute)) { + t.Fatalf("note changed at %v", at) + } + // No clock yet (the pass that admitted the head has not written one): + // the generic caption stands. + bare := stagelabels.Record{Stage: stagelabels.PreMergeSoak, Reason: "contributor-lane:soaking", EnteredAt: entered} + if got := stageTrackerSoakCaption(bare, now); got != "" { + t.Fatalf("no clock: %q", got) + } + if at := stageTrackerNoteChangedAt(bare, now); !at.Equal(entered) { + t.Fatalf("no ReasonChangedAt falls back to EnteredAt: %v", at) + } + // Legacy record: the remaining in the reason, aged from the sweep. + legacy := stagelabels.Record{Stage: stagelabels.PreMergeSoak, Reason: "contributor-lane:soaking(remaining=12m0s)", EnteredAt: entered, LastSeen: now.Add(-5 * time.Minute)} + if got := stageTrackerSoakCaption(legacy, now); got != "Admitted by the automerge lane; soaking — 7m left" { + t.Fatalf("legacy caption = %q", got) + } + if got := stageTrackerSoakCaption(legacy, now.Add(8*time.Minute)); got != "Admitted by the automerge lane; soak complete — merges on the next pass" { + t.Fatalf("legacy past deadline = %q", got) + } + // A note never dates from the future. + if at := stageTrackerNoteChangedAt(stagelabels.Record{Stage: stagelabels.Reviewed, EnteredAt: now.Add(time.Hour)}, now); !at.Equal(now) { + t.Fatalf("future entry clamped: %v", at) + } +} diff --git a/src/pkg/dashboard/stage_tracker_test.go b/src/pkg/dashboard/stage_tracker_test.go index df4c6ce380..d0fcaec7c9 100644 --- a/src/pkg/dashboard/stage_tracker_test.go +++ b/src/pkg/dashboard/stage_tracker_test.go @@ -346,11 +346,14 @@ func stageTrackerFixture(t *testing.T, now time.Time) (*Server, *stagelabels.Sto clock = now.Add(-25 * time.Hour) store.Observe(key101, stagelabels.ReturnedToContributor, "reviewer-verdict:changes_requested") store.MarkNudged(key101, "abc123", 1, true, clock) - // PR 102: hive PR admitted and soaking, fresh (12m left as of a sweep 5m ago). + // PR 102: hive PR admitted and soaking: the record carries the soak + // clock (head green 23m ago, 30m window → 7m left now); the reason is + // the bare token and the sweep re-read it 5m ago. clock = now.Add(-40 * time.Minute) - store.Observe(stagelabels.Key("acme/console", 102), stagelabels.PreMergeSoak, "contributor-lane:soaking(remaining=17m0s)") + store.Observe(stagelabels.Key("acme/console", 102), stagelabels.PreMergeSoak, "contributor-lane:soaking") + store.NoteSoak(stagelabels.Key("acme/console", 102), "abc102", now.Add(-23*time.Minute), 30*time.Minute) clock = now.Add(-5 * time.Minute) - store.Observe(stagelabels.Key("acme/console", 102), stagelabels.PreMergeSoak, "contributor-lane:soaking(remaining=12m0s)") + store.Observe(stagelabels.Key("acme/console", 102), stagelabels.PreMergeSoak, "contributor-lane:soaking") // PR 103: regressed from the merge queue to review-pending. key103 := stagelabels.Key("acme/console", 103) clock = now.Add(-3 * time.Hour) @@ -506,11 +509,16 @@ func TestStageTrackerEndpoint(t *testing.T) { if pr102.Caption != "Admitted by the automerge lane; soaking — 7m left" || pr102.Actor != "automerge lane" || len(pr102.Modifiers) != 0 { t.Fatalf("pr102 caption=%q actor=%q mods=%v", pr102.Caption, pr102.Actor, pr102.Modifiers) } - // 12m left when the sweep wrote it 5m ago → 7m now; the 40m-old entry - // time is the stage_since, not the soak clock. + // The record's deadline (green 23m ago + 30m) → 7m now; the 40m-old + // entry time is the stage_since, not the soak clock. if pr102.SoakRemainingSeconds != 7*60 || pr102.StageSince == nil || !pr102.StageSince.Equal(now.Add(-40*time.Minute)) { t.Fatalf("pr102 soak_remaining_seconds=%d since=%v", pr102.SoakRemainingSeconds, pr102.StageSince) } + // The note's age is the stage entry, not the sweep 5m ago that re-read + // the same reason (hivecommons/hive#15729). + if pr102.NoteChangedAt == nil || !pr102.NoteChangedAt.Equal(now.Add(-40*time.Minute)) { + t.Fatalf("pr102 note_changed_at=%v, want the 095 entry 40m ago", pr102.NoteChangedAt) + } pr103 := trackerItem(t, resp, "pr", 103) if pr103.Stage != 50 || pr103.RegressedFrom != 100 || !hasMod(pr103, modRegressed) || pr103.AuthorKind != authorKindClanker || pr103.Lane != laneContributor { diff --git a/src/pkg/dashboard/static-src/js/tracker-motion.js b/src/pkg/dashboard/static-src/js/tracker-motion.js index eea6dd1224..4c959bc88f 100644 --- a/src/pkg/dashboard/static-src/js/tracker-motion.js +++ b/src/pkg/dashboard/static-src/js/tracker-motion.js @@ -71,7 +71,7 @@ // Pure: key → the fields the event line watches. function stageTrackerStateMap(items) { const out = {}; - const put = (key, it) => { out[key] = { stage: Number(it.stage) || 0, label: it.stage_label || '', caption: it.caption || '', kind: it.kind || '', mirrored: !!it.mirrored, mods: (Array.isArray(it.modifiers) ? it.modifiers : []).slice().sort(), rework: it.rework && it.rework.state ? String(it.rework.state) : '', reworkActor: it.rework && it.rework.actor ? String(it.rework.actor) : '' }; }; + const put = (key, it) => { out[key] = { stage: Number(it.stage) || 0, label: it.stage_label || '', caption: it.caption || '', noteAt: it.note_changed_at || '', kind: it.kind || '', mirrored: !!it.mirrored, mods: (Array.isArray(it.modifiers) ? it.modifiers : []).slice().sort(), rework: it.rework && it.rework.state ? String(it.rework.state) : '', reworkActor: it.rework && it.rework.actor ? String(it.rework.actor) : '' }; }; for (const it of (Array.isArray(items) ? items : [])) { if (!it || !it.kind || !(Number(it.number) > 0)) continue; put(stageTrackerItemKey(it), it); @@ -140,14 +140,44 @@ function stageTrackerEventsNext(events, prevStates, nextStates, stages, nowMs, ttlMs) { const ttl = typeof ttlMs === 'number' ? ttlMs : STAGE_TRACKER_EVENT_MS; const out = {}; - for (const k of Object.keys(events || {})) if (nowMs - events[k].at < ttl && (!prevStates || ttl !== Infinity || nextStates[k])) out[k] = events[k]; + for (const k of Object.keys(events || {})) if (nowMs - stageTrackerEventShownAt(events[k]) < ttl && (!prevStates || ttl !== Infinity || nextStates[k])) out[k] = events[k]; if (!prevStates) return out; for (const k of Object.keys(nextStates)) { - const text = stageTrackerEventText(prevStates[k], nextStates[k], stages); - if (text) out[k] = { who: 'hive', text, at: nowMs }; + const prev = prevStates[k], next = nextStates[k]; + const text = stageTrackerEventText(prev, next, stages); + if (!text) continue; + if (stageTrackerSameNote(prev, next, stages)) { + // The same note with new words (a soak counting down): the + // line keeps its age and its open/tucked state; the words + // move in place. No line to refresh: nothing new to say. + if (out[k] && out[k].who === 'hive') out[k] = Object.assign({}, out[k], { text }); + continue; + } + out[k] = stageTrackerNewEvent(text, next.noteAt, nowMs); } return out; } + // Pure: when this tab first showed (or last re-opened) the line — its + // lifetime and fade count from here; its age (ev.at) may be older when + // the server said when the note changed. + function stageTrackerEventShownAt(ev) { + return ev && typeof ev.openAt === 'number' ? ev.openAt : (ev ? ev.at : 0); + } + // Pure: whether a caption-only change left the note's meaning alone — + // the server's note_changed_at did not move (hivecommons/hive#15729: + // "soaking — 10m left" → "9m left" is not a new hive comment). + function stageTrackerSameNote(prev, next, stages) { + if (!prev || !next || !next.noteAt || next.noteAt !== prev.noteAt) return false; + return !stageTrackerEventText(prev, Object.assign({}, next, { caption: prev.caption }), stages); + } + // Pure: a fresh hive line. Its age counts from when the note changed + // on the server (noteAt, when known and not in the future), its + // 30 s open window from now so it is seen. + function stageTrackerNewEvent(text, noteAt, nowMs) { + const at = noteAt ? Date.parse(noteAt) : NaN; + if (!(at > 0) || at >= nowMs) return { who: 'hive', text, at: nowMs }; + return { who: 'hive', text, at, openAt: nowMs }; + } function stageTrackerNoteEvents(d, nowMs) { nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); const next = stageTrackerStateMap(d && d.items); @@ -289,7 +319,7 @@ if (!ev) return ''; const nowMs = ctx && typeof ctx.nowMs === 'number' ? ctx.nowMs : Date.now(); const auto = ctx && typeof ctx.noteTuck === 'boolean' ? ctx.noteTuck : (typeof stageTrackerNoteTuckSetting === 'function' && stageTrackerNoteTuckSetting()); - const fading = !auto && nowMs - ev.at > STAGE_TRACKER_EVENT_MS * 2 / 3; + const fading = !auto && nowMs - stageTrackerEventShownAt(ev) > STAGE_TRACKER_EVENT_MS * 2 / 3; const open = !auto || stageTrackerNoteOpen(ev, nowMs, auto); const who = ev.who === 'you' ? 'You' : 'Hive'; const body = `${esc(who)}: ${esc(ev.text)} `; diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 06da9262d6..9e2ba324df 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -17688,7 +17688,7 @@

Where to get help

// Pure: key → the fields the event line watches. function stageTrackerStateMap(items) { const out = {}; - const put = (key, it) => { out[key] = { stage: Number(it.stage) || 0, label: it.stage_label || '', caption: it.caption || '', kind: it.kind || '', mirrored: !!it.mirrored, mods: (Array.isArray(it.modifiers) ? it.modifiers : []).slice().sort(), rework: it.rework && it.rework.state ? String(it.rework.state) : '', reworkActor: it.rework && it.rework.actor ? String(it.rework.actor) : '' }; }; + const put = (key, it) => { out[key] = { stage: Number(it.stage) || 0, label: it.stage_label || '', caption: it.caption || '', noteAt: it.note_changed_at || '', kind: it.kind || '', mirrored: !!it.mirrored, mods: (Array.isArray(it.modifiers) ? it.modifiers : []).slice().sort(), rework: it.rework && it.rework.state ? String(it.rework.state) : '', reworkActor: it.rework && it.rework.actor ? String(it.rework.actor) : '' }; }; for (const it of (Array.isArray(items) ? items : [])) { if (!it || !it.kind || !(Number(it.number) > 0)) continue; put(stageTrackerItemKey(it), it); @@ -17757,14 +17757,44 @@

Where to get help

function stageTrackerEventsNext(events, prevStates, nextStates, stages, nowMs, ttlMs) { const ttl = typeof ttlMs === 'number' ? ttlMs : STAGE_TRACKER_EVENT_MS; const out = {}; - for (const k of Object.keys(events || {})) if (nowMs - events[k].at < ttl && (!prevStates || ttl !== Infinity || nextStates[k])) out[k] = events[k]; + for (const k of Object.keys(events || {})) if (nowMs - stageTrackerEventShownAt(events[k]) < ttl && (!prevStates || ttl !== Infinity || nextStates[k])) out[k] = events[k]; if (!prevStates) return out; for (const k of Object.keys(nextStates)) { - const text = stageTrackerEventText(prevStates[k], nextStates[k], stages); - if (text) out[k] = { who: 'hive', text, at: nowMs }; + const prev = prevStates[k], next = nextStates[k]; + const text = stageTrackerEventText(prev, next, stages); + if (!text) continue; + if (stageTrackerSameNote(prev, next, stages)) { + // The same note with new words (a soak counting down): the + // line keeps its age and its open/tucked state; the words + // move in place. No line to refresh: nothing new to say. + if (out[k] && out[k].who === 'hive') out[k] = Object.assign({}, out[k], { text }); + continue; + } + out[k] = stageTrackerNewEvent(text, next.noteAt, nowMs); } return out; } + // Pure: when this tab first showed (or last re-opened) the line — its + // lifetime and fade count from here; its age (ev.at) may be older when + // the server said when the note changed. + function stageTrackerEventShownAt(ev) { + return ev && typeof ev.openAt === 'number' ? ev.openAt : (ev ? ev.at : 0); + } + // Pure: whether a caption-only change left the note's meaning alone — + // the server's note_changed_at did not move (hivecommons/hive#15729: + // "soaking — 10m left" → "9m left" is not a new hive comment). + function stageTrackerSameNote(prev, next, stages) { + if (!prev || !next || !next.noteAt || next.noteAt !== prev.noteAt) return false; + return !stageTrackerEventText(prev, Object.assign({}, next, { caption: prev.caption }), stages); + } + // Pure: a fresh hive line. Its age counts from when the note changed + // on the server (noteAt, when known and not in the future), its + // 30 s open window from now so it is seen. + function stageTrackerNewEvent(text, noteAt, nowMs) { + const at = noteAt ? Date.parse(noteAt) : NaN; + if (!(at > 0) || at >= nowMs) return { who: 'hive', text, at: nowMs }; + return { who: 'hive', text, at, openAt: nowMs }; + } function stageTrackerNoteEvents(d, nowMs) { nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); const next = stageTrackerStateMap(d && d.items); @@ -17906,7 +17936,7 @@

Where to get help

if (!ev) return ''; const nowMs = ctx && typeof ctx.nowMs === 'number' ? ctx.nowMs : Date.now(); const auto = ctx && typeof ctx.noteTuck === 'boolean' ? ctx.noteTuck : (typeof stageTrackerNoteTuckSetting === 'function' && stageTrackerNoteTuckSetting()); - const fading = !auto && nowMs - ev.at > STAGE_TRACKER_EVENT_MS * 2 / 3; + const fading = !auto && nowMs - stageTrackerEventShownAt(ev) > STAGE_TRACKER_EVENT_MS * 2 / 3; const open = !auto || stageTrackerNoteOpen(ev, nowMs, auto); const who = ev.who === 'you' ? 'You' : 'Hive'; const body = `${esc(who)}: ${esc(ev.text)} `; diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index f78a190dc9..81b59d2819 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -386,12 +386,6 @@ func (p ContributorLanePolicy) excludedLabel(labels []string) string { return "" } -// contributorLaneSoakReason renders the soaking skip with the time left so a -// reader of the log or the dashboard knows when the merge is due. -func contributorLaneSoakReason(remaining time.Duration) string { - return fmt.Sprintf("%s(remaining=%s)", contributorLaneReasonSoaking, remaining.Round(time.Minute)) -} - // SweepContributorLaneAutoMerges merges green, soaked, reviewer-approved PRs // that this hive's contributor lane produced. See the file comment for the // full gate list. @@ -1274,6 +1268,14 @@ type contributorLaneEval struct { // workflowApprover is the trusted merger whose approval released the // workflow-path fence for headSHA ("" when the fence did not trip). workflowApprover string + // soakStartedAt / soakRemaining are the soak clock the lane holds + // headSHA on when the skip reason is contributorLaneReasonSoaking: + // when the head was first seen green and how long is left. The reason + // itself carries no countdown (hivecommons/hive#15729: a reason that + // changed every pass re-stamped the stage record and the dashboard + // read each pass as a fresh hive note); the record keeps the deadline. + soakStartedAt time.Time + soakRemaining time.Duration } // evaluateContributorLanePR runs every contributor-lane gate except the @@ -1442,7 +1444,8 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own } firstGreen := c.contributorSoak.observeGreenSince(soakKey, now, greenSince) if soaked := now.Sub(firstGreen); soaked < policy.Soak { - return eval, contributorLaneSoakReason(policy.Soak - soaked), nil + eval.soakStartedAt, eval.soakRemaining = firstGreen, policy.Soak-soaked + return eval, contributorLaneReasonSoaking, nil } if humanReason, err := c.humanMergePathGate(ctx, displayRepo, owner, repo, pr); err != nil || humanReason != "" { return eval, humanReason, err diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index db17b8a562..3db0536a7e 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -1626,22 +1626,34 @@ func TestTrySweepContributorLanePRSoakTiming(t *testing.T) { engine := newContributorLaneEngine(api.URL, policy, clock, Options{ContributorSoak: tracker}) ctx := context.Background() - // First green observation: soak starts now, nothing merges. - _, reason, err := engine.trySweepContributorLanePR(ctx, "widget", "acme", "widget", 7, policy) - if err != nil || reason != contributorLaneReasonSoaking+"(remaining=2h0m0s)" { + // First green observation: soak starts now, nothing merges. The reason + // is the bare token — the countdown is not in it (hivecommons/hive#15729: + // a reason that moved every pass re-stamped the stage record); the + // clock rides on the evaluation. + eval, reason, err := engine.evaluateContributorLanePR(ctx, "widget", "acme", "widget", 7, policy, false) + if err != nil || reason != contributorLaneReasonSoaking { t.Fatalf("first pass reason=%q err=%v", reason, err) } + if !eval.soakStartedAt.Equal(clock.at) || eval.soakRemaining != 2*time.Hour { + t.Fatalf("first pass soak clock = %v / %v, want %v / 2h", eval.soakStartedAt, eval.soakRemaining, clock.at) + } firstGreen, ok := tracker.FirstGreen("acme", "widget", 7, "sha7") if !ok || !firstGreen.Equal(clock.at) { t.Fatalf("first green = %v ok=%v, want %v", firstGreen, ok, clock.at) } - // Half-way through the soak the remaining time is reported. + // Half-way through the soak the reason is unchanged; the clock counts. clock.advance(time.Hour) - _, reason, err = engine.trySweepContributorLanePR(ctx, "widget", "acme", "widget", 7, policy) - if err != nil || reason != contributorLaneReasonSoaking+"(remaining=1h0m0s)" { + eval, reason, err = engine.evaluateContributorLanePR(ctx, "widget", "acme", "widget", 7, policy, false) + if err != nil || reason != contributorLaneReasonSoaking { t.Fatalf("mid-soak reason=%q err=%v", reason, err) } + if !eval.soakStartedAt.Equal(firstGreen) || eval.soakRemaining != time.Hour { + t.Fatalf("mid-soak clock = %v / %v, want %v / 1h", eval.soakStartedAt, eval.soakRemaining, firstGreen) + } + if _, reason, err = engine.trySweepContributorLanePR(ctx, "widget", "acme", "widget", 7, policy); err != nil || reason != contributorLaneReasonSoaking { + t.Fatalf("mid-soak sweep reason=%q err=%v", reason, err) + } if got, _ := tracker.FirstGreen("acme", "widget", 7, "sha7"); !got.Equal(firstGreen) { t.Fatalf("first green moved to %v", got) } @@ -1671,9 +1683,9 @@ func TestTrySweepContributorLanePRSoakSurvivesRestart(t *testing.T) { engine := newContributorLaneEngine(api.URL, policy, clock, Options{ContributorSoak: tracker}) ctx := context.Background() - _, reason, err := engine.trySweepContributorLanePR(ctx, "widget", "acme", "widget", 7, policy) - if err != nil || reason != contributorLaneReasonSoaking+"(remaining=30m0s)" { - t.Fatalf("fresh process reason=%q err=%v, want 30m remaining from the check's completed_at", reason, err) + eval, reason, err := engine.evaluateContributorLanePR(ctx, "widget", "acme", "widget", 7, policy, false) + if err != nil || reason != contributorLaneReasonSoaking || eval.soakRemaining != 30*time.Minute { + t.Fatalf("fresh process reason=%q remaining=%v err=%v, want 30m remaining from the check's completed_at", reason, eval.soakRemaining, err) } if first, ok := tracker.FirstGreen("acme", "widget", 7, "sha7"); !ok || !first.Equal(clock.at.Add(-90*time.Minute)) { t.Fatalf("first green = %v ok=%v, want seeded from completed_at", first, ok) @@ -1692,8 +1704,8 @@ func TestTrySweepContributorLanePRSoakSurvivesRestart(t *testing.T) { api2 := newContributorLaneAPI(t, &fx2) defer api2.Close() engine2 := newContributorLaneEngine(api2.URL, policy, clock, Options{ContributorSoak: NewContributorSoakTracker()}) - if _, reason, err := engine2.trySweepContributorLanePR(ctx, "widget", "acme", "widget", 7, policy); err != nil || reason != contributorLaneReasonSoaking+"(remaining=2h0m0s)" { - t.Fatalf("future completed_at reason=%q err=%v", reason, err) + if eval, reason, err := engine2.evaluateContributorLanePR(ctx, "widget", "acme", "widget", 7, policy, false); err != nil || reason != contributorLaneReasonSoaking || eval.soakRemaining != 2*time.Hour { + t.Fatalf("future completed_at reason=%q remaining=%v err=%v", reason, eval.soakRemaining, err) } } @@ -1861,9 +1873,6 @@ func TestContributorLanePolicyHelpers(t *testing.T) { if got := p.excludedLabel([]string{"ok"}); got != "" { t.Fatalf("excludedLabel = %q, want empty", got) } - if got := contributorLaneSoakReason(90*time.Minute + 20*time.Second); got != "contributor-lane:soaking(remaining=1h30m0s)" { - t.Fatalf("soak reason = %q", got) - } var nilEngine *Engine if nilEngine.currentContributorLanePolicy().Enabled { t.Fatalf("nil engine policy should be disabled") diff --git a/src/pkg/github/automerge/automerge_relay_gate.go b/src/pkg/github/automerge/automerge_relay_gate.go index 64c5387c78..6dfe4f0c90 100644 --- a/src/pkg/github/automerge/automerge_relay_gate.go +++ b/src/pkg/github/automerge/automerge_relay_gate.go @@ -19,6 +19,7 @@ import ( "fmt" "sort" "strings" + "time" hgithub "github.com/hivecommons/hive/pkg/github" ) @@ -73,9 +74,9 @@ func (c *Engine) RelayMergeRefusal(ctx context.Context, displayRepo string, numb policy := c.currentContributorLanePolicy() switch { case !policy.Enabled: - return relayMergeRefusal(author, "", policy, relayLaneReasonDisabled), nil + return relayMergeRefusal(author, "", policy, relayLaneReasonDisabled, 0), nil case !policy.repoAllowed(displayRepo): - return relayMergeRefusal(author, "", policy, relayLaneReasonRepo), nil + return relayMergeRefusal(author, "", policy, relayLaneReasonRepo, 0), nil } ctx = contextWithExpectedCheckCache(ctx, newExpectedCheckCache()) eval, reason, err := c.evaluateContributorLanePR(ctx, displayRepo, owner, repo, number, policy, true) @@ -89,15 +90,17 @@ func (c *Engine) RelayMergeRefusal(ctx context.Context, displayRepo string, numb c.info("merge relay lane gate: contributor lane admits PR", "repo", displayRepo, "pr", number, "author", author, "admitted_by", contributorLaneAdmitPath(eval.claim)) return "", nil } - return relayMergeRefusal(author, eval.claim.ObservedTier, policy, reason), nil + return relayMergeRefusal(author, eval.claim.ObservedTier, policy, reason, eval.soakRemaining), nil } // relayMergeRefusal renders the refusal the relay records: the lane's own // skip reason first (so it matches the sweep log and the stage label), then // what the operator can act on — the author, the tier the lane saw against // require_tiers, and the soak — and that the lane merges the PR itself once -// every gate passes, so nobody re-requests it. -func relayMergeRefusal(author, observedTier string, policy ContributorLanePolicy, reason string) string { +// every gate passes, so nobody re-requests it. soakLeft is the soak time +// left when reason is the soaking skip (the reason itself carries no +// countdown), 0 otherwise. +func relayMergeRefusal(author, observedTier string, policy ContributorLanePolicy, reason string, soakLeft time.Duration) string { var b strings.Builder b.WriteString(RelayMergeRefusalPrefix) if author != "" { @@ -110,7 +113,11 @@ func relayMergeRefusal(author, observedTier string, policy ContributorLanePolicy case reason == relayLaneReasonRepo: b.WriteString(" (this repo is outside auto_merge.contributor_lane.repos)") case strings.HasPrefix(reason, contributorLaneReasonSoaking): - fmt.Fprintf(&b, " (auto_merge.contributor_lane soak is %s; the lane merges it itself once the soak elapses)", policy.Soak) + fmt.Fprintf(&b, " (auto_merge.contributor_lane soak is %s", policy.Soak) + if soakLeft > 0 { + fmt.Fprintf(&b, ", %s remaining", soakLeft.Round(time.Minute)) + } + b.WriteString("; the lane merges it itself once the soak elapses)") case strings.HasPrefix(reason, "pending: head pushed "): b.WriteString(" (auto_merge.min_head_age; the lane merges it itself once the head is old enough)") default: diff --git a/src/pkg/github/automerge/automerge_relay_gate_test.go b/src/pkg/github/automerge/automerge_relay_gate_test.go index d23403e624..0edbc193bd 100644 --- a/src/pkg/github/automerge/automerge_relay_gate_test.go +++ b/src/pkg/github/automerge/automerge_relay_gate_test.go @@ -70,7 +70,7 @@ func TestRelayMergeRefusalSoakAndHeadAge(t *testing.T) { if err != nil { t.Fatalf("err = %v", err) } - for _, want := range []string{contributorLaneReasonSoaking, "remaining=30m", "soak is 30m0s"} { + for _, want := range []string{contributorLaneReasonSoaking, "30m0s remaining", "soak is 30m0s"} { if !strings.Contains(refusal, want) { t.Fatalf("refusal %q does not name %q", refusal, want) } @@ -255,12 +255,12 @@ func TestRelayMergeRefusalFailsClosed(t *testing.T) { func TestRelayMergeRefusalText(t *testing.T) { policy := ContributorLanePolicy{Soak: 30 * time.Minute, RequireTiers: map[string]bool{"trusted": true, "merger": true}} - got := relayMergeRefusal("Karibhou", "untrusted", policy, contributorLaneReasonClaimTier) + got := relayMergeRefusal("Karibhou", "untrusted", policy, contributorLaneReasonClaimTier, 0) want := RelayMergeRefusalPrefix + ` for @Karibhou's PR: contributor-lane:claim-tier-untrusted (author tier "untrusted", require_tiers merger, trusted)` if got != want { t.Fatalf("got %q\nwant %q", got, want) } - if got := relayMergeRefusal("", "", ContributorLanePolicy{}, contributorLaneReasonNoClaim); !strings.Contains(got, `tier "none", require_tiers (none)`) { + if got := relayMergeRefusal("", "", ContributorLanePolicy{}, contributorLaneReasonNoClaim, 0); !strings.Contains(got, `tier "none", require_tiers (none)`) { t.Fatalf("got %q", got) } } diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index b02624a933..ccab943322 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -1113,7 +1113,8 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) // list "PRs returned to this contributor" from the store alone. store.NoteAuthor(key, obs.Author) store.NoteHold(key, obs.Hold) - noteSoak(store, key, obs.Reason, obs.HeadSHA, pass.Route, c.currentContributorLanePolicy().Soak, now) + soakFirstGreen, _ := c.contributorSoak.FirstGreen(obs.Owner, obs.Repo, obs.Number, obs.HeadSHA) + noteSoak(store, key, obs.Reason, obs.HeadSHA, pass.Route, c.currentContributorLanePolicy().Soak, soakFirstGreen, now) if dcoRead { store.NoteDCO(key, obs.HeadSHA, obs.Detail.DCOTotal, obs.Detail.DCOUnsigned, obs.Detail.DCOUnsignedHive, obs.Detail.DCOSquashUnsafeHive) } diff --git a/src/pkg/github/automerge/automerge_stage_soak.go b/src/pkg/github/automerge/automerge_stage_soak.go index d355dc9c8b..a65df61e19 100644 --- a/src/pkg/github/automerge/automerge_stage_soak.go +++ b/src/pkg/github/automerge/automerge_stage_soak.go @@ -4,23 +4,31 @@ package automerge // (ContributorSoakTracker) is in-memory and per head; the stage record is // what the dashboard reads. Each pass writes the soak it is holding the // PR's head for — the lane's own soak after admission (095, -// `contributor-lane:soaking(remaining=…)`) or the soak a trust-blocked head -// sits through at 060 (RouteHuman's "soak Nm remaining") — so the Activity -// Monitor can count one clock down in both places and tell a head that -// moved on from one still soaking. +// `contributor-lane:soaking`, with the clock read from the tracker) or the +// soak a trust-blocked head sits through at 060 (RouteHuman's "soak Nm +// remaining") — so the Activity Monitor can count one clock down in both +// places and tell a head that moved on from one still soaking. The clock +// is a fact about the head (when it went green, the window), so an +// unchanged pass writes nothing: the record stays byte-identical and the +// dashboard does not read every sweep as a fresh hive note +// (hivecommons/hive#15729). import ( "regexp" + "strings" "time" "github.com/hivecommons/hive/pkg/stagelabels" ) -// soakRemainingRE pulls the time left out of the lane's -// `contributor-lane:soaking(remaining=12m0s)` reason. +// soakRemainingRE pulls the time left out of the +// `contributor-lane:soaking(remaining=12m0s)` reason the lane wrote before +// the clock moved onto the record. Kept so a stored reason of that shape +// still reads as a clock; nothing writes it any more. var soakRemainingRE = regexp.MustCompile(`remaining=([0-9hms.]+)`) -// soakRemainingFromReason parses the soak time left from a soaking reason. +// soakRemainingFromReason parses the soak time left from a soaking reason +// of the pre-record shape; false for the bare `contributor-lane:soaking`. func soakRemainingFromReason(reason string) (time.Duration, bool) { m := soakRemainingRE.FindStringSubmatch(reason) if m == nil { @@ -34,17 +42,28 @@ func soakRemainingFromReason(reason string) (time.Duration, bool) { } // soakWindowFor derives the soak the pass is holding head for from the -// lane's reason and the human-route evidence: the head, when it was first -// green and the window. ok is false when the head is not soaking. -func soakWindowFor(reason, head string, route *stagelabels.Evidence, window time.Duration, now time.Time) (startedAt time.Time, win time.Duration, ok bool) { +// lane's reason, the tracker's first-green time for the head (firstGreen; +// zero when the tracker has not seen it) and the human-route evidence: +// the head, when it was first green and the window. ok is false when the +// head is not soaking. +func soakWindowFor(reason, head string, route *stagelabels.Evidence, window time.Duration, firstGreen, now time.Time) (startedAt time.Time, win time.Duration, ok bool) { if head == "" { return time.Time{}, 0, false } - if left, parsed := soakRemainingFromReason(reason); parsed && window > 0 { - if left > window { - left = window + if strings.HasPrefix(reason, contributorLaneReasonSoaking) && window > 0 { + if left, parsed := soakRemainingFromReason(reason); parsed { + if left > window { + left = window + } + return now.Add(left - window), window, true } - return now.Add(left - window), window, true + if !firstGreen.IsZero() && !firstGreen.After(now) { + return firstGreen, window, true + } + // The lane says soaking but this process has no clock for the + // head (the tracker is in memory): the soak reads as starting + // now, and noteSoak keeps a clock already on the record instead. + return now, window, true } // RouteHuman measures the soak only on a green head (SoakElapsed is 0 // both when the head is not green and when it was first seen green @@ -63,15 +82,26 @@ func routeHeadGreen(route *stagelabels.Evidence) bool { return c.Known && c.Total > 0 && c.Pending == 0 && len(c.Red) == 0 } -// noteSoak writes (or clears) the record's soak for this pass. -func noteSoak(store *stagelabels.Store, key, reason, head string, route *stagelabels.Evidence, window time.Duration, now time.Time) { +// noteSoak writes (or clears) the record's soak for this pass. A clock +// the record already carries for the same head is kept when the tracker +// has none to offer (firstGreen zero): the record is the memory that +// survives a restart, and re-stamping it from "now" would restart the +// countdown the dashboard shows. +func noteSoak(store *stagelabels.Store, key, reason, head string, route *stagelabels.Evidence, window time.Duration, firstGreen, now time.Time) { if store == nil { return } - startedAt, win, ok := soakWindowFor(reason, head, route, window, now) + startedAt, win, ok := soakWindowFor(reason, head, route, window, firstGreen, now) if !ok { store.ClearSoak(key) return } + if firstGreen.IsZero() && strings.HasPrefix(reason, contributorLaneReasonSoaking) { + if rec, has := store.Get(key); has { + if soakHead, at, recWin, recorded := rec.Soak(); recorded && soakHead == head && recWin == win && !at.After(now) { + startedAt = at + } + } + } store.NoteSoak(key, head, startedAt, win) } diff --git a/src/pkg/github/automerge/automerge_stage_soak_test.go b/src/pkg/github/automerge/automerge_stage_soak_test.go index 81cb6c730a..f5cf7d5b41 100644 --- a/src/pkg/github/automerge/automerge_stage_soak_test.go +++ b/src/pkg/github/automerge/automerge_stage_soak_test.go @@ -9,37 +9,60 @@ import ( ) // TestSoakWindowFor: the soak the pass records for a head — the lane's -// `soaking(remaining=…)` reason after admission (095), RouteHuman's soak -// evidence on a green trust-blocked head (060/080, including a head first -// seen green this pass and one past the window), and nothing for a head -// that is not green, has no soak, or is blank. +// bare `contributor-lane:soaking` reason with the tracker's first-green +// time (095), the pre-record `soaking(remaining=…)` reason still read for +// stored records, RouteHuman's soak evidence on a green trust-blocked head +// (060/080, including a head first seen green this pass and one past the +// window), and nothing for a head that is not green, has no soak, or is +// blank. func TestSoakWindowFor(t *testing.T) { now := time.Date(2026, 10, 28, 20, 30, 0, 0, time.UTC) window := 30 * time.Minute green := stagelabels.CheckSummary{Total: 3, Green: 3, Known: true} + var noClock time.Time - at, win, ok := soakWindowFor("contributor-lane:soaking(remaining=12m0s)", "abc", nil, window, now) + // 095 with the tracker's clock: the soak started when the head went green. + firstGreen := now.Add(-18 * time.Minute) + at, win, ok := soakWindowFor("contributor-lane:soaking", "abc", nil, window, firstGreen, now) + if !ok || win != window || !at.Equal(firstGreen) { + t.Fatalf("095 reason + tracker: %v %v %v", at, win, ok) + } + // Same inputs a pass later: the same clock (nothing to re-stamp). + if at2, _, _ := soakWindowFor("contributor-lane:soaking", "abc", nil, window, firstGreen, now.Add(time.Minute)); !at2.Equal(at) { + t.Fatalf("clock moved between passes: %v → %v", at, at2) + } + // Tracker cold (restart): the soak reads as starting now; noteSoak + // keeps a record's own clock in that case (TestNoteSoakKeepsRecordedClock). + if at, _, ok = soakWindowFor("contributor-lane:soaking", "abc", nil, window, noClock, now); !ok || !at.Equal(now) { + t.Fatalf("cold tracker: %v %v", at, ok) + } + // A first-green in the future (clock skew) is not trusted either. + if at, _, ok = soakWindowFor("contributor-lane:soaking", "abc", nil, window, now.Add(time.Minute), now); !ok || !at.Equal(now) { + t.Fatalf("future first green: %v %v", at, ok) + } + // The pre-record reason shape still reads as a clock, aged from its remaining. + at, win, ok = soakWindowFor("contributor-lane:soaking(remaining=12m0s)", "abc", nil, window, noClock, now) if !ok || win != window || !at.Equal(now.Add(-18*time.Minute)) { - t.Fatalf("095 reason: %v %v %v", at, win, ok) + t.Fatalf("legacy 095 reason: %v %v %v", at, win, ok) } // Remaining longer than the window (config shrank): clamp to the window. - if at, _, ok = soakWindowFor("contributor-lane:soaking(remaining=45m0s)", "abc", nil, window, now); !ok || !at.Equal(now) { + if at, _, ok = soakWindowFor("contributor-lane:soaking(remaining=45m0s)", "abc", nil, window, noClock, now); !ok || !at.Equal(now) { t.Fatalf("clamped: %v %v", at, ok) } // The reason without a configured window is not a clock. - if _, _, ok = soakWindowFor("contributor-lane:soaking(remaining=12m0s)", "abc", nil, 0, now); ok { + if _, _, ok = soakWindowFor("contributor-lane:soaking", "abc", nil, 0, firstGreen, now); ok { t.Fatal("no window: must not soak") } route := &stagelabels.Evidence{Soak: window, SoakElapsed: 5 * time.Minute, Checks: green} - if at, win, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", route, window, now); !ok || win != window || !at.Equal(now.Add(-5*time.Minute)) { + if at, win, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", route, window, noClock, now); !ok || win != window || !at.Equal(now.Add(-5*time.Minute)) { t.Fatalf("060 route: %v %v %v", at, win, ok) } first := &stagelabels.Evidence{Soak: window, Checks: green} - if at, _, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", first, window, now); !ok || !at.Equal(now) { + if at, _, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", first, window, noClock, now); !ok || !at.Equal(now) { t.Fatalf("first seen green this pass must start the clock now: %v %v", at, ok) } done := &stagelabels.Evidence{Soak: window, SoakElapsed: 40 * time.Minute, Checks: green} - if _, _, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", done, window, now); !ok { + if _, _, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", done, window, noClock, now); !ok { t.Fatal("a head past the window still carries its clock") } for name, e := range map[string]*stagelabels.Evidence{ @@ -49,11 +72,11 @@ func TestSoakWindowFor(t *testing.T) { "no-ci": {Soak: window, Checks: stagelabels.CheckSummary{Known: true}}, "no-soak": {Checks: green, SoakElapsed: time.Minute}, } { - if _, _, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", e, window, now); ok { + if _, _, ok = soakWindowFor("contributor-lane:claim-tier-untrusted", "abc", e, window, noClock, now); ok { t.Errorf("%s must not soak", name) } } - if _, _, ok = soakWindowFor("contributor-lane:soaking(remaining=12m0s)", "", nil, window, now); ok { + if _, _, ok = soakWindowFor("contributor-lane:soaking", "", nil, window, firstGreen, now); ok { t.Fatal("blank head must not soak") } } @@ -64,15 +87,57 @@ func TestNoteSoakWritesAndClears(t *testing.T) { now := time.Date(2026, 10, 28, 20, 30, 0, 0, time.UTC) store := stagelabels.NewStore(filepath.Join(t.TempDir(), "stage.json"), func() time.Time { return now }) key := stagelabels.Key("acme/widget", 15174) - store.Observe(key, stagelabels.PreMergeSoak, "contributor-lane:soaking(remaining=12m0s)") - noteSoak(store, key, "contributor-lane:soaking(remaining=12m0s)", "abc1234def", nil, 30*time.Minute, now) + firstGreen := now.Add(-18 * time.Minute) + store.Observe(key, stagelabels.PreMergeSoak, "contributor-lane:soaking") + noteSoak(store, key, "contributor-lane:soaking", "abc1234def", nil, 30*time.Minute, firstGreen, now) rec, _ := store.Get(key) - if head, at, win, ok := rec.Soak(); !ok || head != "abc1234def" || win != 30*time.Minute || !at.Equal(now.Add(-18*time.Minute)) { + if head, at, win, ok := rec.Soak(); !ok || head != "abc1234def" || win != 30*time.Minute || !at.Equal(firstGreen) { t.Fatalf("soak = %q %v %v ok=%v", head, at, win, ok) } - noteSoak(store, key, "contributor-lane:merged", "abc1234def", nil, 30*time.Minute, now) + if err := store.Save(); err != nil { + t.Fatal(err) + } + // The next pass, same head, same clock: the record is untouched and + // the store has nothing to write (hivecommons/hive#15729). + now = now.Add(time.Minute) + store.Observe(key, stagelabels.PreMergeSoak, "contributor-lane:soaking") + noteSoak(store, key, "contributor-lane:soaking", "abc1234def", nil, 30*time.Minute, firstGreen, now) + if again, _ := store.Get(key); !again.SoakStartedAt.Equal(rec.SoakStartedAt) || again.SoakHead != rec.SoakHead || again.SoakWindowSeconds != rec.SoakWindowSeconds { + t.Fatalf("unchanged pass re-stamped the soak: %+v → %+v", rec, again) + } + if store.Dirty() { + t.Fatal("an unchanged soaking pass must not dirty the store") + } + noteSoak(store, key, "contributor-lane:merged", "abc1234def", nil, 30*time.Minute, firstGreen, now) if rec, _ := store.Get(key); rec.SoakHead != "" { t.Fatalf("a pass that is not soaking must clear: %+v", rec) } - noteSoak(nil, key, "contributor-lane:soaking(remaining=12m0s)", "abc", nil, 30*time.Minute, now) // nil store: no panic + noteSoak(nil, key, "contributor-lane:soaking", "abc", nil, 30*time.Minute, firstGreen, now) // nil store: no panic +} + +// TestNoteSoakKeepsRecordedClock: after a restart the in-memory tracker +// has no first-green for the head, but the record does; the pass keeps +// the record's clock rather than restarting the countdown from now. A +// different head, or a record with no clock, starts from now. +func TestNoteSoakKeepsRecordedClock(t *testing.T) { + now := time.Date(2026, 10, 28, 20, 30, 0, 0, time.UTC) + store := stagelabels.NewStore(filepath.Join(t.TempDir(), "stage.json"), func() time.Time { return now }) + key := stagelabels.Key("acme/widget", 15730) + started := now.Add(-20 * time.Minute) + store.Observe(key, stagelabels.PreMergeSoak, "contributor-lane:soaking") + store.NoteSoak(key, "abc", started, 30*time.Minute) + var cold time.Time + noteSoak(store, key, "contributor-lane:soaking", "abc", nil, 30*time.Minute, cold, now) + if rec, _ := store.Get(key); !rec.SoakStartedAt.Equal(started) { + t.Fatalf("cold tracker restarted the recorded clock: %v, want %v", rec.SoakStartedAt, started) + } + noteSoak(store, key, "contributor-lane:soaking", "def", nil, 30*time.Minute, cold, now) + if rec, _ := store.Get(key); rec.SoakHead != "def" || !rec.SoakStartedAt.Equal(now) { + t.Fatalf("a new head must start its own clock: %+v", rec) + } + // The tracker knowing better wins over the record. + noteSoak(store, key, "contributor-lane:soaking", "def", nil, 30*time.Minute, now.Add(-5*time.Minute), now) + if rec, _ := store.Get(key); !rec.SoakStartedAt.Equal(now.Add(-5 * time.Minute)) { + t.Fatalf("tracker clock must replace the provisional one: %+v", rec) + } } diff --git a/src/pkg/stagelabels/soak_test.go b/src/pkg/stagelabels/soak_test.go index 725e25615f..b871e90264 100644 --- a/src/pkg/stagelabels/soak_test.go +++ b/src/pkg/stagelabels/soak_test.go @@ -1,6 +1,8 @@ package stagelabels import ( + "fmt" + "os" "path/filepath" "testing" "time" @@ -62,3 +64,122 @@ func TestStoreNoteSoak(t *testing.T) { t.Fatalf("zero window must clear, got %+v", rec) } } + +// TestObserveUnchangedPassDoesNotDirty (hivecommons/hive#15729): a pass +// that re-observes the same stage and reason moves LastSeen in memory but +// leaves the saved file alone until LastSeenPersistEvery has passed; any +// other change still dirties it, and a reason of a new class stamps +// ReasonChangedAt. +func TestObserveUnchangedPassDoesNotDirty(t *testing.T) { + now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC) + s := NewStore(filepath.Join(t.TempDir(), "stage.json"), func() time.Time { return now }) + key := Key("acme/widget", 15730) + s.Observe(key, PreMergeSoak, "contributor-lane:soaking") + if err := s.Save(); err != nil { + t.Fatal(err) + } + entered := now + for i := 1; i <= 5; i++ { + now = entered.Add(time.Duration(i) * time.Minute) + if tr := s.Observe(key, PreMergeSoak, "contributor-lane:soaking"); tr.Changed { + t.Fatalf("pass %d: unchanged observation reported a change", i) + } + if s.Dirty() { + t.Fatalf("pass %d: an unchanged pass dirtied the store", i) + } + rec, _ := s.Get(key) + if !rec.LastSeen.Equal(now) { + t.Fatalf("pass %d: LastSeen = %v, want %v (the live store keeps the exact sweep time)", i, rec.LastSeen, now) + } + if !rec.ReasonChangedAt.Equal(entered) || !rec.NoteChangedAt().Equal(entered) { + t.Fatalf("pass %d: note changed at %v, want stage entry %v", i, rec.ReasonChangedAt, entered) + } + } + // Once the saved LastSeen is LastSeenPersistEvery old the pass writes it. + now = entered.Add(LastSeenPersistEvery) + s.Observe(key, PreMergeSoak, "contributor-lane:soaking") + if !s.Dirty() { + t.Fatal("a LastSeen older than LastSeenPersistEvery on disk must be persisted") + } + if err := s.Save(); err != nil { + t.Fatal(err) + } + now = now.Add(time.Minute) + s.Observe(key, PreMergeSoak, "contributor-lane:soaking") + if s.Dirty() { + t.Fatal("the pass after a save is unchanged again") + } + // A reason of a new class at the same stage is a change: the note moved. + now = now.Add(time.Minute) + if tr := s.Observe(key, PreMergeSoak, "release-hold"); tr.Changed { + t.Fatal("a reason change is not a stage change") + } + if !s.Dirty() { + t.Fatal("a new reason must dirty the store") + } + if rec, _ := s.Get(key); !rec.ReasonChangedAt.Equal(now) || !rec.NoteChangedAt().Equal(now) { + t.Fatalf("ReasonChangedAt = %v, want %v", rec.ReasonChangedAt, now) + } + // NoteActivity with nothing new leaves a clean store clean. + if err := s.Save(); err != nil { + t.Fatal(err) + } + a := Activity{Head: "abc", Comments: 2, Reviews: 1, Digest: "d"} + s.NoteActivity(key, a) + if !s.Dirty() { + t.Fatal("the first activity observation must be written") + } + if err := s.Save(); err != nil { + t.Fatal(err) + } + s.NoteActivity(key, a) + if s.Dirty() { + t.Fatal("re-observing the same activity must not dirty the store") + } +} + +// TestReasonClassAndLegacySoakRecord: the pre-#15729 soak reason carried +// its countdown (`soaking(remaining=12m0s)`), so two passes differ only in +// the detail; ReasonClass reads them as one note, and a record written +// that way loads and does not stamp ReasonChangedAt when the bare reason +// replaces it. +func TestReasonClassAndLegacySoakRecord(t *testing.T) { + for in, want := range map[string]string{ + "contributor-lane:soaking(remaining=12m0s)": "contributor-lane:soaking", + "contributor-lane:soaking": "contributor-lane:soaking", + " check-failing:unit ": "check-failing:unit", + "": "", + } { + if got := ReasonClass(in); got != want { + t.Errorf("ReasonClass(%q) = %q, want %q", in, got, want) + } + } + now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC) + path := filepath.Join(t.TempDir(), "stage.json") + legacy := `{"acme/widget#15730":{"stage":` + fmt.Sprint(int(PreMergeSoak)) + `,"entered_at":"2026-10-30T11:40:00Z","reason":"contributor-lane:soaking(remaining=12m0s)","last_seen":"2026-10-30T11:58:00Z"}}` + if err := os.WriteFile(path, []byte(legacy), 0o644); err != nil { + t.Fatal(err) + } + s := NewStore(path, func() time.Time { return now }) + if err := s.Load(); err != nil { + t.Fatal(err) + } + key := Key("acme/widget", 15730) + rec, ok := s.Get(key) + if !ok || rec.Stage != PreMergeSoak || !rec.ReasonChangedAt.IsZero() || !rec.NoteChangedAt().Equal(rec.EnteredAt) { + t.Fatalf("legacy record = %+v ok=%v", rec, ok) + } + if _, _, _, soaking := rec.Soak(); soaking { + t.Fatal("a legacy record carries no soak clock of its own") + } + // The first pass of the new code writes the bare reason: same class, + // so the note did not change; the store is dirty for the reason text. + s.Observe(key, PreMergeSoak, "contributor-lane:soaking") + rec, _ = s.Get(key) + if rec.Reason != "contributor-lane:soaking" || !rec.ReasonChangedAt.IsZero() { + t.Fatalf("after upgrade: %+v", rec) + } + if !s.Dirty() { + t.Fatal("the reason text changed; the store must be written once") + } +} diff --git a/src/pkg/stagelabels/store.go b/src/pkg/stagelabels/store.go index 312ee7598c..932ab9129a 100644 --- a/src/pkg/stagelabels/store.go +++ b/src/pkg/stagelabels/store.go @@ -51,6 +51,13 @@ type Record struct { StallCommented bool `json:"stall_commented,omitempty"` // Reason is the lane/CI/verdict reason behind the current stage. Reason string `json:"reason,omitempty"` + // ReasonChangedAt is when Reason last changed meaning: stage entry, or + // a pass that observed a different reason class (ReasonClass) at the + // same stage. The dashboard ages the row's hive note from it rather + // than from LastSeen, so a pass that re-reads the same reason does not + // read as a fresh note (hivecommons/hive#15729). Zero on records + // written before it existed; NoteChangedAt falls back to EnteredAt. + ReasonChangedAt time.Time `json:"reason_changed_at,omitempty"` // Author is the item's author login, lower-cased, as the lane saw it // (hivecommons/hive#12502). It lets the dashboard answer "which of this // contributor's PRs are returned to them" from the store alone, with no @@ -487,6 +494,10 @@ type Store struct { now func() time.Time items map[string]Record dirty bool + // seenSaved is each record's LastSeen as last written to (or read + // from) the file; ObserveProgress dirties the store for a LastSeen-only + // move once the saved value is LastSeenPersistEvery old. + seenSaved map[string]time.Time } // NewStore returns an empty in-memory store that persists to path ("" = @@ -495,7 +506,16 @@ func NewStore(path string, now func() time.Time) *Store { if now == nil { now = time.Now } - return &Store{path: path, now: now, items: map[string]Record{}} + return &Store{path: path, now: now, items: map[string]Record{}, seenSaved: map[string]time.Time{}} +} + +// markSeenSaved notes the LastSeen the file now holds for every record. +// Caller holds s.mu. +func (s *Store) markSeenSaved() { + s.seenSaved = make(map[string]time.Time, len(s.items)) + for k, r := range s.items { + s.seenSaved[k] = r.LastSeen + } } var ( @@ -542,6 +562,7 @@ func (s *Store) Load() error { items = map[string]Record{} } s.items = items + s.markSeenSaved() s.dirty = false return nil } @@ -570,10 +591,21 @@ func (s *Store) Save() error { if err := os.Rename(tmp, s.path); err != nil { return err } + s.markSeenSaved() s.dirty = false return nil } +// Dirty reports whether anything changed since the last Save or Load. +func (s *Store) Dirty() bool { + if s == nil { + return false + } + s.mu.Lock() + defer s.mu.Unlock() + return s.dirty +} + // Get returns the record for key. func (s *Store) Get(key string) (Record, bool) { s.mu.Lock() @@ -615,9 +647,12 @@ func (s *Store) ObserveProgress(key string, stage Stage, reason string, progress next.LastSeen = now if !had || prev.Stage != stage { next.EnteredAt = now + next.ReasonChangedAt = now next.StallCommented = false next.EntryCommented = false tr.Changed = true + } else if ReasonClass(prev.Reason) != ReasonClass(reason) { + next.ReasonChangedAt = now } switch { case had && prev.Stage != None && !progress && IsRegression(prev.Stage, stage): @@ -649,20 +684,53 @@ func (s *Store) ObserveProgress(key string, stage Stage, reason string, progress if tr.Changed { next.History = appendHop(prev.History, Hop{From: prev.Stage, Stage: stage, At: now, Reason: reason, Regressed: tr.Regressed}) } - if !had || tr.Changed || !recordEqual(next, prev) { + if !had || tr.Changed || !recordEqual(next, prev) || now.Sub(s.seenSaved[key]) >= LastSeenPersistEvery { s.dirty = true } s.items[key] = next return tr } -// recordEqual compares two records field by field; History (a slice) makes -// Record incomparable with ==, and it only changes when the stage does. +// LastSeenPersistEvery bounds how stale a record's persisted LastSeen may +// be when a pass changes nothing else about it. A pass that re-observes +// the same stage and reason moves LastSeen in memory (the dashboard reads +// the live store, so "last checked" stays exact) but does not by itself +// dirty the store (hivecommons/hive#15729: a soaking PR rewrote the file +// every sweep); once the saved LastSeen is this old the pass writes it so +// Prune stays roughly right across a restart. +const LastSeenPersistEvery = 10 * time.Minute + +// recordEqual compares two records field by field, ignoring History (a +// slice that makes Record incomparable with ==, and only changes when the +// stage does) and LastSeen (which moves on every pass and is persisted on +// its own schedule, LastSeenPersistEvery). func recordEqual(a, b Record) bool { a.History, b.History = nil, nil + a.LastSeen, b.LastSeen = time.Time{}, time.Time{} return reflect.DeepEqual(a, b) } +// ReasonClass is the part of a reason that names what it means: the token +// up to any parenthesised detail, so `contributor-lane:soaking(remaining=12m0s)` +// (the pre-record soak reason) and `contributor-lane:soaking` are one +// class. Two passes whose reasons share a class did not change the note. +func ReasonClass(reason string) string { + reason = strings.TrimSpace(reason) + if i := strings.IndexByte(reason, '('); i >= 0 { + reason = reason[:i] + } + return reason +} + +// NoteChangedAt is when the record's note — its stage and reason class — +// last changed: ReasonChangedAt when recorded, else EnteredAt. +func (r Record) NoteChangedAt() time.Time { + if !r.ReasonChangedAt.IsZero() { + return r.ReasonChangedAt + } + return r.EnteredAt +} + // NoteActivity folds this cycle's activity inputs into key's record and // reports whether anything a person could have done changed since the last // observation. The first observation of a record seeds LastActivityAt @@ -709,8 +777,10 @@ func (s *Store) NoteActivity(key string, a Activity) (changed bool) { } r.ActivityDigest = a.Digest r.WaitingOnHive = a.WaitingOnHive + if !recordEqual(r, s.items[key]) { + s.dirty = true + } s.items[key] = r - s.dirty = true return changed }