From e2d4d53c2df6982484b3a10427a8e9cd3f4e4e10 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 28 Oct 2026 18:05:53 -0400 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20Persist=20a=20merged=20ledge?= =?UTF-8?q?r=20under=20/data=20for=20the=20Activity=20Monitor=20Merged=20c?= =?UTF-8?q?hip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Server side of hivecommons/hive#15096: every tracked PR that reaches merged (hive, human or other bot; audit log, webhook or closed sweep) is recorded in /data/merged-ledger.json, seeded from audit history on first boot, and summarised on /api/stage-tracker as merged{today, total_7d, total_30d, all_time, recent}. Signed-off-by: Andrew Anderson --- src/pkg/dashboard/api.go | 1 + src/pkg/dashboard/audit.go | 21 +- src/pkg/dashboard/merged_ledger.go | 378 ++++++++++++++++++ src/pkg/dashboard/merged_ledger_server.go | 201 ++++++++++ src/pkg/dashboard/server.go | 8 +- src/pkg/dashboard/stage_tracker.go | 6 + .../dashboard/stage_tracker_closed_sweep.go | 8 +- src/pkg/dashboard/webhook_tracker_refresh.go | 1 + src/pkg/github/closed_since.go | 5 + src/pkg/github/webhook_item_refresh.go | 29 +- 10 files changed, 644 insertions(+), 14 deletions(-) create mode 100644 src/pkg/dashboard/merged_ledger.go create mode 100644 src/pkg/dashboard/merged_ledger_server.go diff --git a/src/pkg/dashboard/api.go b/src/pkg/dashboard/api.go index a3b9059f37..245776cc79 100644 --- a/src/pkg/dashboard/api.go +++ b/src/pkg/dashboard/api.go @@ -29,6 +29,7 @@ func (s *Server) RegisterAPI(deps *Dependencies) { } s.deps = deps s.loadSidebarFromDisk() + s.mergedLedger() s.registerContributeRoutes() // Issue claims (hivecommons/hive#8380). Routes register unconditionally; // handlers answer "not enabled" when deps.IssueClaims is nil. diff --git a/src/pkg/dashboard/audit.go b/src/pkg/dashboard/audit.go index 2c2aa46e51..b379f67e78 100644 --- a/src/pkg/dashboard/audit.go +++ b/src/pkg/dashboard/audit.go @@ -17,6 +17,7 @@ import ( "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/dashboard/collect" + "github.com/hivecommons/hive/pkg/github" "gopkg.in/natefinch/lumberjack.v2" ) @@ -165,6 +166,18 @@ type AuditLog struct { // they persist; "" keeps them in memory only (no /data volume, tests). prCounters PRThroughputCounters prCountersPath string + // onPRMerged, when set, is called (outside the lock) with each pr_merged + // entry the log accepts — the first record of a PR's merge, the + // duplicates having been dropped by notePRThroughput. The merged ledger + // (merged_ledger.go) is its consumer. + onPRMerged func(AuditEntry) +} + +// SetPRMergedHook installs the pr_merged observer; nil removes it. +func (a *AuditLog) SetPRMergedHook(fn func(AuditEntry)) { + a.mu.Lock() + a.onPRMerged = fn + a.mu.Unlock() } func newAuditLog() *AuditLog { @@ -249,12 +262,11 @@ func (a *AuditLog) logRecordAt(timestamp, user, action, detail, agent, repo stri } a.mu.Lock() - defer a.mu.Unlock() - if len(a.ring) >= auditRingCap { a.ring = a.ring[1:] } if prThroughputActions[entry.Action] && !a.notePRThroughput(entry) && prThroughputTerminalAction(entry.Action) { + a.mu.Unlock() return } a.ring = append(a.ring, entry) @@ -267,6 +279,11 @@ func (a *AuditLog) logRecordAt(timestamp, user, action, detail, agent, repo stri } } } + onMerged := a.onPRMerged + a.mu.Unlock() + if onMerged != nil && entry.Action == github.AuditActionPRMerged { + onMerged(entry) + } } // noteUserAction records ts as user's most recent audited action, skipping diff --git a/src/pkg/dashboard/merged_ledger.go b/src/pkg/dashboard/merged_ledger.go new file mode 100644 index 0000000000..6d29648ff4 --- /dev/null +++ b/src/pkg/dashboard/merged_ledger.go @@ -0,0 +1,378 @@ +package dashboard + +import ( + "encoding/json" + "log/slog" + "os" + "sort" + "strconv" + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/pkg/dashboard/collect" + "github.com/hivecommons/hive/pkg/github" +) + +// The merged ledger is the Activity Monitor's server-side record of every +// pull request the hive tracked that reached merged — whoever merged it +// (hivecommons/hive#15096). Before it, the toolbar's "Merged today" chip was +// accumulated in the tab from rows vanishing as merged and seeded from the +// in-memory audit ring on reload, so a refresh, a new tab or a roll of the +// pod reset it (the operator watched 4 become 0, and 33 become 4). +// +// Sources, all funnelled through recordMerged and deduplicated per PR: +// +// - the audit log's pr_merged entries — the hive's own merges on every path +// (RecordPRMergedAudit) and the PR poller's observations of human and +// third-party-bot merges (the PRTerminalObservedHook in cmd/hive); +// - webhook / closed-sweep snapshots of a PR GitHub reports merged +// (applyWebhookItemSnapshots), which arrive minutes before the poller's +// settle scan and carry the title and the merger's login. +// +// The file under /data survives a roll; entries are kept for +// mergedLedgerRetention (≥ 30 days) while the all-time total is a counter +// that only ever grows. On first boot with this feature the ledger seeds +// itself from the on-disk audit history and the durable PR throughput +// counters, so the chip does not start again from zero. + +// mergedLedgerPath is where the ledger persists; tests point it elsewhere. +var mergedLedgerPath = "/data/merged-ledger.json" + +const ( + // mergedLedgerRetention bounds how long an entry stays listed. The + // 30-day window the API reports needs 30; 90 matches the audit log's + // own MaxAge so the two never disagree about the recent past. + mergedLedgerRetention = 90 * 24 * time.Hour + // mergedLedgerMaxEntries caps the file on a very busy hive. + mergedLedgerMaxEntries = 5000 + // mergedLedgerRecentMax is how many entries the API's recent list carries. + mergedLedgerRecentMax = 20 + mergedLedgerVersion = 1 + // MergedByHive is the merged_by value for a merge the hive performed + // itself (sweep, queue, relay or forge path). Any other value is the + // merger's GitHub login; "" means not known. + MergedByHive = "hive" +) + +// MergedLedgerEntry is one merged pull request. +type MergedLedgerEntry struct { + Repo string `json:"repo"` + Number int `json:"number"` + Title string `json:"title,omitempty"` + URL string `json:"url,omitempty"` + MergedAt time.Time `json:"merged_at"` + // MergedBy is MergedByHive or the merger's login ("" unknown). + MergedBy string `json:"merged_by,omitempty"` + // Lane is the tracker lane the PR was on when it merged (issue, hive, + // contributor), Issue the issue it was nested under; both best effort. + Lane string `json:"lane,omitempty"` + Issue int `json:"issue,omitempty"` +} + +func (e MergedLedgerEntry) key() string { return mergedLedgerKey(e.Repo, e.Number) } + +func mergedLedgerKey(repo string, number int) string { + return strings.ToLower(strings.TrimSpace(repo)) + "#" + strconv.Itoa(number) +} + +// StageTrackerMergedSummary is the `merged` block of /api/stage-tracker. +type StageTrackerMergedSummary struct { + // Today counts merges since midnight in TZ; Total7d / Total30d the + // rolling windows; AllTime the counter that survives everything. + Today int `json:"today"` + Total7d int `json:"total_7d"` + Total30d int `json:"total_30d"` + AllTime int `json:"all_time"` + // Recent lists the newest mergedLedgerRecentMax entries, newest first. + Recent []MergedLedgerEntry `json:"recent"` + // TZ names the zone "today" is computed in; DayStart is that midnight. + TZ string `json:"tz"` + DayStart time.Time `json:"day_start"` +} + +type mergedLedgerFile struct { + Version int `json:"version"` + AllTime int `json:"all_time"` + Entries []MergedLedgerEntry `json:"entries"` +} + +// MergedLedger is the persistent record; the zero value is unusable, use +// newMergedLedger. +type MergedLedger struct { + mu sync.Mutex + path string + allTime int + entries []MergedLedgerEntry // oldest first + index map[string]int + // fromDisk is true when the constructor found a file: the ledger has + // history and must not be seeded again. + fromDisk bool + // onNew is called (without the lock) after a PR is recorded for the + // first time; the server uses it to drop the tracker cache. + onNew func(MergedLedgerEntry) +} + +// newMergedLedger loads the ledger at path ("" keeps it in memory only). +func newMergedLedger(path string) *MergedLedger { + l := &MergedLedger{path: path, index: map[string]int{}} + if path == "" { + return l + } + data, err := os.ReadFile(path) + if err != nil { + return l + } + var f mergedLedgerFile + if json.Unmarshal(data, &f) != nil { + return l + } + l.fromDisk = true + l.allTime = f.AllTime + for _, e := range f.Entries { + if e.Repo == "" || e.Number <= 0 { + continue + } + l.upsertLocked(e) + } + l.sortLocked() + return l +} + +// Seeded reports whether the ledger already has history (a file was found +// or Seed ran), so a boot seeds at most once. +func (l *MergedLedger) Seeded() bool { + l.mu.Lock() + defer l.mu.Unlock() + return l.fromDisk +} + +// Seed folds existing merge history in on first boot: the entries are +// recorded (deduplicated) and the all-time total becomes at least floor — +// the durable PR throughput counter, which may remember merges whose audit +// lines have rotated away. It persists and marks the ledger seeded. +func (l *MergedLedger) Seed(entries []MergedLedgerEntry, floor int, now time.Time) { + l.mu.Lock() + defer l.mu.Unlock() + for _, e := range entries { + if e.Repo == "" || e.Number <= 0 { + continue + } + if _, dup := l.index[e.key()]; !dup { + l.allTime++ + } + l.upsertLocked(e) + } + if l.allTime < floor { + l.allTime = floor + } + l.fromDisk = true + l.sortLocked() + l.pruneLocked(now) + l.persistLocked() +} + +// Record adds a merged PR, or fills in what an earlier record of the same +// PR did not know (title, merger, lane, issue). It reports whether the PR +// was new to the ledger; only a new PR bumps the all-time total. +func (l *MergedLedger) Record(e MergedLedgerEntry, now time.Time) bool { + if l == nil || e.Repo == "" || e.Number <= 0 { + return false + } + if e.MergedAt.IsZero() { + e.MergedAt = now + } + l.mu.Lock() + _, dup := l.index[e.key()] + if !dup { + l.allTime++ + } + l.upsertLocked(e) + l.sortLocked() + l.pruneLocked(now) + l.persistLocked() + onNew := l.onNew + l.mu.Unlock() + if !dup && onNew != nil { + onNew(e) + } + return !dup +} + +// upsertLocked inserts e or merges it into the existing entry for the same +// PR: blank fields are filled, a known field is never overwritten, except +// that the earliest merge time wins (a snapshot's updated_at can trail the +// real merge). +func (l *MergedLedger) upsertLocked(e MergedLedgerEntry) { + e.Repo = strings.TrimSpace(e.Repo) + e.MergedAt = e.MergedAt.UTC() + i, ok := l.index[e.key()] + if !ok { + l.index[e.key()] = len(l.entries) + l.entries = append(l.entries, e) + return + } + cur := &l.entries[i] + if cur.Title == "" { + cur.Title = e.Title + } + if cur.URL == "" { + cur.URL = e.URL + } + if cur.MergedBy == "" { + cur.MergedBy = e.MergedBy + } + if cur.Lane == "" { + cur.Lane = e.Lane + } + if cur.Issue == 0 { + cur.Issue = e.Issue + } + if !e.MergedAt.IsZero() && (cur.MergedAt.IsZero() || e.MergedAt.Before(cur.MergedAt)) { + cur.MergedAt = e.MergedAt + } +} + +func (l *MergedLedger) sortLocked() { + sort.SliceStable(l.entries, func(i, j int) bool { return l.entries[i].MergedAt.Before(l.entries[j].MergedAt) }) + for i := range l.entries { + l.index[l.entries[i].key()] = i + } +} + +// pruneLocked drops entries past retention and the oldest beyond the cap; +// the all-time total is untouched. Entries must be sorted. +func (l *MergedLedger) pruneLocked(now time.Time) { + cutoff := now.Add(-mergedLedgerRetention) + drop := 0 + for drop < len(l.entries) && l.entries[drop].MergedAt.Before(cutoff) { + drop++ + } + if over := len(l.entries) - drop - mergedLedgerMaxEntries; over > 0 { + drop += over + } + if drop == 0 { + return + } + for _, e := range l.entries[:drop] { + delete(l.index, e.key()) + } + l.entries = append([]MergedLedgerEntry(nil), l.entries[drop:]...) + for i := range l.entries { + l.index[l.entries[i].key()] = i + } +} + +// persistLocked writes the file atomically (temp + rename), as the PR +// throughput counters do, so a crash cannot leave a torn file. +func (l *MergedLedger) persistLocked() { + if l.path == "" { + return + } + data, err := json.Marshal(mergedLedgerFile{Version: mergedLedgerVersion, AllTime: l.allTime, Entries: l.entries}) + if err != nil { + return + } + tmp := l.path + ".tmp" + if err := os.WriteFile(tmp, data, 0o644); err != nil { + slog.Error("merged ledger write failed", "error", err) + return + } + if err := os.Rename(tmp, l.path); err != nil { + slog.Error("merged ledger rename failed", "error", err) + } +} + +// Summary computes the API block at now in loc (nil → UTC). "Today" rolls +// at midnight in loc; the 7- and 30-day totals are rolling windows. +func (l *MergedLedger) Summary(now time.Time, loc *time.Location) StageTrackerMergedSummary { + if loc == nil { + loc = time.UTC + } + local := now.In(loc) + dayStart := time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, loc) + tz := loc.String() + if tz == "Local" { + tz = local.Format("MST") + } + out := StageTrackerMergedSummary{Recent: []MergedLedgerEntry{}, TZ: tz, DayStart: dayStart} + if l == nil { + return out + } + since7 := now.Add(-7 * 24 * time.Hour) + since30 := now.Add(-30 * 24 * time.Hour) + l.mu.Lock() + defer l.mu.Unlock() + out.AllTime = l.allTime + for i := len(l.entries) - 1; i >= 0; i-- { + e := l.entries[i] + if e.MergedAt.After(now.Add(time.Minute)) { + continue + } + if !e.MergedAt.Before(dayStart) { + out.Today++ + } + if !e.MergedAt.Before(since7) { + out.Total7d++ + } + if !e.MergedAt.Before(since30) { + out.Total30d++ + } + if len(out.Recent) < mergedLedgerRecentMax { + out.Recent = append(out.Recent, e) + } + } + return out +} + +// Len is the number of listed (unpruned) entries. +func (l *MergedLedger) Len() int { + l.mu.Lock() + defer l.mu.Unlock() + return len(l.entries) +} + +// mergedEntryFromAudit projects a pr_merged audit entry onto the ledger. +// qualify maps a short repo name onto owner/name. ok is false when the +// entry names no PR. +func mergedEntryFromAudit(e AuditEntry, qualify func(string) string) (MergedLedgerEntry, bool) { + if e.Action != github.AuditActionPRMerged { + return MergedLedgerEntry{}, false + } + repo, ok := collect.AuditEntryRepo(e) + if !ok { + return MergedLedgerEntry{}, false + } + if qualify != nil { + repo = qualify(repo) + } + attrs := parseAuditDetailAttrs(e.Detail) + number := e.Target + if number == 0 { + number, _ = strconv.Atoi(attrs["number"]) + } + if number <= 0 { + return MergedLedgerEntry{}, false + } + at, err := time.Parse(time.RFC3339, e.Timestamp) + if err != nil { + return MergedLedgerEntry{}, false + } + out := MergedLedgerEntry{Repo: repo, Number: number, MergedAt: at.UTC(), MergedBy: mergedByFromAudit(attrs)} + return out, true +} + +// mergedByFromAudit reads who merged from a pr_merged detail: the hive's +// own paths (sweep, queue, relay, forge, hive) are MergedByHive; an observed +// merge names its actor login; anything else is unknown. +func mergedByFromAudit(attrs map[string]string) string { + switch strings.ToLower(strings.TrimSpace(attrs["path"])) { + case github.PRAuditPathSweep, github.PRAuditPathQueue, github.PRAuditPathRelay, github.PRAuditPathForge, MergedByHive: + return MergedByHive + } + actor := strings.TrimSpace(attrs["actor"]) + if actor == "" || strings.EqualFold(actor, "unknown") { + return "" + } + return actor +} diff --git a/src/pkg/dashboard/merged_ledger_server.go b/src/pkg/dashboard/merged_ledger_server.go new file mode 100644 index 0000000000..3be66a86db --- /dev/null +++ b/src/pkg/dashboard/merged_ledger_server.go @@ -0,0 +1,201 @@ +package dashboard + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "time" + + "github.com/hivecommons/hive/pkg/github" +) + +// mergedLedger returns the server's merged ledger, building it on first +// use: the file under /data when that volume exists (memory only +// otherwise), seeded from the audit history on a first boot, and wired to +// the audit log so every accepted pr_merged entry lands in it. +func (s *Server) mergedLedger() *MergedLedger { + if s == nil { + return nil + } + s.mergedOnce.Do(func() { + path := mergedLedgerPath + if _, err := os.Stat(filepath.Dir(path)); err != nil { + path = "" + } + l := newMergedLedger(path) + l.onNew = func(MergedLedgerEntry) { s.invalidateStageTrackerCache() } + s.merged = l + if s.audit != nil { + if !l.Seeded() { + s.seedMergedLedger(l, time.Now()) + } + s.audit.SetPRMergedHook(s.recordMergedAudit) + } + }) + return s.merged +} + +// seedMergedLedger folds the existing merge history in once: every +// pr_merged line in the audit files (plus the in-memory ring, which is all +// a hive without /data has), with the durable PR throughput counter's +// merged total as the all-time floor. +func (s *Server) seedMergedLedger(l *MergedLedger, now time.Time) { + qualify := s.mergedRepoQualifier() + seen := map[string]bool{} + var entries []MergedLedgerEntry + add := func(es []AuditEntry) { + for _, e := range es { + entry, ok := mergedEntryFromAudit(e, qualify) + if !ok || seen[entry.key()] { + continue + } + seen[entry.key()] = true + entries = append(entries, s.enrichMergedEntry(entry)) + } + } + add(s.audit.ActionsWithPrefixSince(time.Time{}, github.AuditActionPRMerged, "")) + add(s.audit.RecentWithPrefixSince(time.Time{}, github.AuditActionPRMerged)) + l.Seed(entries, s.audit.PRThroughputCounters().Merged, now) + if s.logger != nil { + s.logger.Info("merged ledger seeded from audit history", "entries", len(entries), "all_time", l.Summary(now, nil).AllTime) + } +} + +// mergedRepoQualifier maps the short repo names audit entries carry onto +// owner/name, from the status snapshot's repos and the configured org. +func (s *Server) mergedRepoQualifier() func(string) string { + s.statusMu.RLock() + status := s.status + s.statusMu.RUnlock() + byStatus := stageTrackerRepoQualifier(status) + org := "" + if s.deps != nil && s.deps.Config != nil { + org = s.deps.Config.Project.Org + } + return func(repo string) string { + q := byStatus(repo) + if !strings.Contains(q, "/") && org != "" { + return org + "/" + q + } + return q + } +} + +// recordMergedAudit is the audit log's pr_merged observer. +func (s *Server) recordMergedAudit(e AuditEntry) { + entry, ok := mergedEntryFromAudit(e, s.mergedRepoQualifier()) + if !ok { + return + } + s.mergedLedger().Record(s.enrichMergedEntry(entry), time.Now()) +} + +// recordMergedSnapshots records every merged PR among webhook / sweep +// snapshots; it is what makes a human's merge show up within seconds +// rather than at the poller's next settle scan. +func (s *Server) recordMergedSnapshots(snaps []github.WebhookItemSnapshot) { + if s == nil || len(snaps) == 0 { + return + } + now := time.Now() + qualify := s.mergedRepoQualifier() + for _, snap := range snaps { + if !snap.IsPR || !snap.Merged || snap.Number <= 0 || snap.Repo == "" { + continue + } + entry := MergedLedgerEntry{Repo: qualify(snap.Repo), Number: snap.Number, Title: snap.Title, URL: snap.URL, MergedAt: snap.MergedAt} + if entry.MergedAt.IsZero() { + entry.MergedAt = snap.UpdatedAt + } + switch { + case snap.MergedByHive: + entry.MergedBy = MergedByHive + default: + entry.MergedBy = strings.TrimSpace(snap.MergedBy) + } + s.mergedLedger().Record(s.enrichMergedEntry(entry), now) + } +} + +// enrichMergedEntry fills the title, URL, lane and issue from what the +// tracker last knew about the PR (the cached /api/stage-tracker body, then +// the status snapshot). Best effort: a PR the tracker never drew keeps +// whatever the source knew. +func (s *Server) enrichMergedEntry(e MergedLedgerEntry) MergedLedgerEntry { + if s == nil || (e.Title != "" && e.Lane != "" && e.Issue != 0 && e.URL != "") { + return e + } + if it, ok := s.stageTrackerCachedPR(e.Repo, e.Number); ok { + if e.Title == "" { + e.Title = it.Title + } + if e.URL == "" { + e.URL = it.URL + } + if e.Lane == "" { + e.Lane = it.Lane + } + if e.Issue == 0 { + e.Issue = it.NestedUnder + } + } + if e.Title == "" || e.URL == "" { + s.statusMu.RLock() + status := s.status + s.statusMu.RUnlock() + if status != nil { + for _, repo := range status.Repos { + if !strings.EqualFold(overviewRepoName(repo), e.Repo) && !strings.EqualFold(repo.Name, e.Repo) { + continue + } + for _, it := range repoOpenItems(repo) { + if it.Kind != "pr" || it.Number != e.Number { + continue + } + title, url := it.PR.pr.Title, it.PR.pr.URL + if it.Sidelined { + title, url = it.WorkPR.Title, it.WorkPR.URL + } + if e.Title == "" { + e.Title = title + } + if e.URL == "" { + e.URL = url + } + } + } + } + } + return e +} + +// stageTrackerCachedPR finds a PR row in the last tracker body served. +// The body is decoded on demand: merges are rare next to tracker polls, +// and the response struct is not retained. +func (s *Server) stageTrackerCachedPR(repo string, number int) (StageTrackerItem, bool) { + s.stageTrackerCacheMu.Lock() + body := s.stageTrackerCache.body + s.stageTrackerCacheMu.Unlock() + if len(body) == 0 { + return StageTrackerItem{}, false + } + var resp struct { + Items []StageTrackerItem `json:"items"` + } + if json.Unmarshal(body, &resp) != nil { + return StageTrackerItem{}, false + } + for _, it := range resp.Items { + if it.Kind == "pr" && it.Number == number && strings.EqualFold(it.Repo, repo) { + return it, true + } + } + return StageTrackerItem{}, false +} + +// mergedSummaryNow is the API's merged block: "today" in the spoke's +// configured zone (the process TZ; UTC when unset). +func (s *Server) mergedSummaryNow(now time.Time) StageTrackerMergedSummary { + return s.mergedLedger().Summary(now, time.Local) +} diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index 001af108df..d25f9af47a 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -162,8 +162,12 @@ type Server struct { // closedSweep is the tracker's closed-item backstop // (stage_tracker_closed_sweep.go). closedSweep closedSweepState - sidebar interface{} - sidebarMu sync.RWMutex + // merged is the persistent record of merged PRs behind the tracker's + // "Merged" chip (merged_ledger.go); built lazily by mergedLedger. + merged *MergedLedger + mergedOnce sync.Once + sidebar interface{} + sidebarMu sync.RWMutex // startedAt marks process start, used by /api/livez to bound the // startup-grace window before the first heartbeat has to have succeeded. diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index bd3dc39c3b..99ec6d87c2 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -92,6 +92,11 @@ type StageTrackerResponse struct { // the entries since the viewer's local midnight). Best effort: the // in-memory audit ring is the only source, as for RecentlyMerged. Merged24h []StageTrackerMerged `json:"merged_24h,omitempty"` + // Merged is the durable merge ledger's summary (merged_ledger.go): + // counts for today (in the spoke's zone), 7 and 30 days, all time, and + // the last few merges. Unlike RecentlyMerged it survives a roll, so it + // is what the "Merged" chip shows. + Merged StageTrackerMergedSummary `json:"merged"` } // StageTrackerMerged is one recently merged pull request. @@ -1668,6 +1673,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto qualify := stageTrackerRepoQualifier(status) resp.RecentlyMerged = s.stageTrackerRecentlyMerged(now, qualify) resp.Merged24h = s.stageTrackerMergedSince(now, stageTrackerMergedDayWindow, qualify) + resp.Merged = s.mergedSummaryNow(now) if status == nil { return resp } diff --git a/src/pkg/dashboard/stage_tracker_closed_sweep.go b/src/pkg/dashboard/stage_tracker_closed_sweep.go index 466536f2f4..f4cd225bea 100644 --- a/src/pkg/dashboard/stage_tracker_closed_sweep.go +++ b/src/pkg/dashboard/stage_tracker_closed_sweep.go @@ -125,7 +125,7 @@ func (s *Server) sweepClosedRows(ctx context.Context, now time.Time) (int, error } var firstErr error allRead := true - var evict []github.WebhookItemSnapshot + var evict, merged []github.WebhookItemSnapshot for _, repo := range status.Repos { if ctx.Err() != nil { return 0, ctx.Err() @@ -146,11 +146,17 @@ func (s *Server) sweepClosedRows(ctx context.Context, now time.Time) (int, error if !strings.EqualFold(snap.Repo, name) && !strings.EqualFold(snap.Repo, repo.Name) { snap.Repo = name } + if snap.IsPR && snap.Merged { + merged = append(merged, snap) + } if frontendRepoLists(repo, snap.Number, snap.IsPR) { evict = append(evict, snap) } } } + // Every merged PR feeds the ledger, listed or not: a PR that left the + // tracker before the sweep saw it close still counts as merged. + s.recordMergedSnapshots(merged) evicted := 0 if len(evict) > 0 { evicted = s.applyWebhookItemSnapshots(evict) diff --git a/src/pkg/dashboard/webhook_tracker_refresh.go b/src/pkg/dashboard/webhook_tracker_refresh.go index 62a38d7a05..2bd5ef64c2 100644 --- a/src/pkg/dashboard/webhook_tracker_refresh.go +++ b/src/pkg/dashboard/webhook_tracker_refresh.go @@ -351,6 +351,7 @@ func (s *Server) applyWebhookItemSnapshots(snaps []github.WebhookItemSnapshot) i if len(snaps) == 0 { return 0 } + s.recordMergedSnapshots(snaps) if s.deps != nil && s.deps.ApplyWebhookItems != nil { s.deps.ApplyWebhookItems(snaps) } diff --git a/src/pkg/github/closed_since.go b/src/pkg/github/closed_since.go index ba16582fda..69ff710865 100644 --- a/src/pkg/github/closed_since.go +++ b/src/pkg/github/closed_since.go @@ -78,6 +78,11 @@ func (c *Client) ListClosedSince(ctx context.Context, repo string, since time.Ti if snap.State == "" { snap.State = "closed" } + if snap.IsPR { + // The issues feed carries the merge instant, not the merger. + snap.MergedAt = issue.GetPullRequestLinks().GetMergedAt().Time + snap.Merged = !snap.MergedAt.IsZero() + } if snap.Open() { // state=closed should never list an open item; never evict on it. continue diff --git a/src/pkg/github/webhook_item_refresh.go b/src/pkg/github/webhook_item_refresh.go index 75101ac4cc..279f745e84 100644 --- a/src/pkg/github/webhook_item_refresh.go +++ b/src/pkg/github/webhook_item_refresh.go @@ -32,15 +32,21 @@ var ErrRateLimitCaution = errors.New("github rate-limit caution window engaged: // actionable snapshot, so both can be patched without re-enumerating the // repository. type WebhookItemSnapshot struct { - Repo string `json:"repo"` - Number int `json:"number"` - IsPR bool `json:"is_pr"` - Title string `json:"title"` - Author string `json:"author,omitempty"` - Labels []string `json:"labels"` - Draft bool `json:"draft,omitempty"` - State string `json:"state"` - Merged bool `json:"merged,omitempty"` + Repo string `json:"repo"` + Number int `json:"number"` + IsPR bool `json:"is_pr"` + Title string `json:"title"` + Author string `json:"author,omitempty"` + Labels []string `json:"labels"` + Draft bool `json:"draft,omitempty"` + State string `json:"state"` + Merged bool `json:"merged,omitempty"` + // MergedAt and MergedBy are GitHub's merge instant and merger login for + // a merged PR; MergedByHive is true when that login is the hive's own + // identity or App bot (the dashboard's merged ledger reads all three). + MergedAt time.Time `json:"merged_at,omitzero"` + MergedBy string `json:"merged_by,omitempty"` + MergedByHive bool `json:"merged_by_hive,omitempty"` UpdatedAt time.Time `json:"updated_at,omitzero"` URL string `json:"url,omitempty"` AppAuthored bool `json:"app_authored,omitempty"` @@ -109,6 +115,11 @@ func (c *Client) FetchWebhookItemSnapshot(ctx context.Context, repo string, numb snap.Draft = pr.GetDraft() snap.State = pr.GetState() snap.Merged = pr.GetMerged() || !pr.GetMergedAt().IsZero() + if snap.Merged { + snap.MergedAt = pr.GetMergedAt().Time + snap.MergedBy = safeGetLogin(pr.GetMergedBy()) + snap.MergedByHive = prTerminalMergeAttribution(snap.MergedBy, c.getHiveIdentity(), c.appBotLogin) == "hive" + } snap.UpdatedAt = pr.GetUpdatedAt().Time snap.URL = pr.GetHTMLURL() snap.HeadSHA = prHeadSHA(pr) From 83ee708a1d507d707c4711528d4b0f5a4e0b1565 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 28 Oct 2026 18:36:24 -0400 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=90=9B=20Merged=20chip:=20hydrate=20f?= =?UTF-8?q?rom=20the=20ledger,=20sticky=20bar,=20banners,=20bee=20flight,?= =?UTF-8?q?=20tests=20and=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Activity Monitor's chip is now "Merged" (all-time, with today / this week beneath), pinned at the top of the tracker with the column header strip, hydrated from /api/stage-tracker's merged block so a reload shows the server's number and never goes backwards within a tab, with a popover of recent merges. A merge raises a bee (or +1, hive.tracker.celebrate_glyph) from the row's check cell to the chip when it is in view, else off the top of the screen, and posts a dismissible bottom-right banner with Show row. Test celebration closes the dialog and plays on a row in the viewport. Go tests cover ledger persistence, upsert, retention and cap, today's rollover per zone, seeding, the audit projection, the server hooks and the API shape; node fixture tests cover hydration, the flight target and the banners. Docs and changelog updated. Fixes #15147 Signed-off-by: Andrew Anderson --- changelog.d/fixed-merged-ledger.md | 1 + src/docs/api-reference.md | 2 +- src/docs/dashboard-sections.md | 4 +- src/pkg/dashboard/merged_ledger.go | 2 +- src/pkg/dashboard/merged_ledger_test.go | 364 ++++++++++++ src/pkg/dashboard/stage_tracker.go | 8 +- .../stage_tracker_actor_icon_test.go | 103 ++-- src/pkg/dashboard/stage_tracker_cues_test.go | 308 ++++++++-- src/pkg/dashboard/static/index.html | 526 +++++++++++++----- 9 files changed, 1059 insertions(+), 259 deletions(-) create mode 100644 changelog.d/fixed-merged-ledger.md create mode 100644 src/pkg/dashboard/merged_ledger_test.go diff --git a/changelog.d/fixed-merged-ledger.md b/changelog.d/fixed-merged-ledger.md new file mode 100644 index 0000000000..7b14ca1ef0 --- /dev/null +++ b/changelog.d/fixed-merged-ledger.md @@ -0,0 +1 @@ +- The Activity Monitor's merge count no longer resets on reload: a persistent merged ledger under `/data` records every pull request the hive tracked that reached merged (whoever merged it) and `/api/stage-tracker` carries `merged{today, total_7d, total_30d, all_time, recent}`; the chip, now **Merged** (all-time, with today / this week beneath), is pinned at the top of the tracker with the column header strip, opens a popover of recent merges, and a merge raises a bee (or "+1") from the row's ✓ cell to the chip or off the top of the screen plus a dismissible bottom-right banner with a *Show row* link ([#15147](https://github.com/hivecommons/hive/issues/15147)). diff --git a/src/docs/api-reference.md b/src/docs/api-reference.md index 99a28ee13d..6e6ebb948e 100644 --- a/src/docs/api-reference.md +++ b/src/docs/api-reference.md @@ -571,7 +571,7 @@ always resolved server-side from the validated token. | `GET` | `/api/budget-ignore` | Dashboard auth/session | Budget Ignore Get | `pkg/dashboard/api.go:171` | | `POST` | `/api/budget-ignore` | Owner only | Budget Ignore Set | `pkg/dashboard/api.go:172` | | `GET` | `/api/summaries` | Dashboard auth/session | Summaries | `pkg/dashboard/api.go:184` | -| `GET` | `/api/stage-tracker` | Dashboard auth/session | Stage Tracker: every open issue and pull request of the watched repos for the Hive activity monitor — actionable, held, claimed, in-progress, covered-by-PR, filtered (`needs-*`), draft and exempt-labelled items alike, each with `stage`, `stage_since`, `labels`, `created_at`, `author_kind`, `modifiers`, (PRs) `sentinel_alert` / `sentinel_findings` / `sentinel_reason` when the sentinel flagged the PR, `decision_needed` on a row parked on a person (085 / 045 / 015, the needs-human pill; mirrored onto the issue's `linked_prs[]` entry): `{question, asked_by, since, blocker, blocker_reason, options: [{action, label, enabled, reason_if_disabled}]}` — the reviewer's own decision question when the verdict record has one, the blocker in plain words, and the applicable actions under the row context menu's action ids (`decide:delegate` first), and (issues) `linked_prs`; items with no stage yet report stage `0` "Open"; `needs_human_labels` is the "a person must act" label set behind the monitor's Needs human facet (the same list as `/api/status.labelPresets[needs-human]`); `recently_merged` lists the PRs (`repo`, `number`, `merged_at`) whose `pr_merged` audit entry is under 30 minutes old, so a client diffing two payloads can tell a merge from a close. Served from a server-side cache rebuilt at most every `dashboard.stage_tracker.cache_ttl_s` (default `governor.eval_interval_s`) from the spoke's existing snapshot — a request never calls GitHub. Carries `generated_at`, `source` (`cache` or `fresh`), `cache_ttl_s`, an `ETag` and `Cache-Control: private, max-age=`; `If-None-Match` yields 304. Each hit is recorded under REST caller `dashboard:stage-tracker` so `/api/gh-rate-limits` shows it charged nothing. | `pkg/dashboard/api.go:185` | +| `GET` | `/api/stage-tracker` | Dashboard auth/session | Stage Tracker: every open issue and pull request of the watched repos for the Hive activity monitor — actionable, held, claimed, in-progress, covered-by-PR, filtered (`needs-*`), draft and exempt-labelled items alike, each with `stage`, `stage_since`, `labels`, `created_at`, `author_kind`, `modifiers`, (PRs) `sentinel_alert` / `sentinel_findings` / `sentinel_reason` when the sentinel flagged the PR, `decision_needed` on a row parked on a person (085 / 045 / 015, the needs-human pill; mirrored onto the issue's `linked_prs[]` entry): `{question, asked_by, since, blocker, blocker_reason, options: [{action, label, enabled, reason_if_disabled}]}` — the reviewer's own decision question when the verdict record has one, the blocker in plain words, and the applicable actions under the row context menu's action ids (`decide:delegate` first), and (issues) `linked_prs`; items with no stage yet report stage `0` "Open"; `needs_human_labels` is the "a person must act" label set behind the monitor's Needs human facet (the same list as `/api/status.labelPresets[needs-human]`); `recently_merged` lists the PRs (`repo`, `number`, `merged_at`) whose `pr_merged` audit entry is under 30 minutes old, so a client diffing two payloads can tell a merge from a close; `merged` is the durable merged ledger's summary — `{today, total_7d, total_30d, all_time, recent: [≤20 × {repo, number, title, url, merged_at, merged_by (`hive` or a login), lane, issue}], tz, day_start}`, `today` rolling at midnight in the spoke's zone — behind the monitor's Merged chip. Served from a server-side cache rebuilt at most every `dashboard.stage_tracker.cache_ttl_s` (default `governor.eval_interval_s`) from the spoke's existing snapshot — a request never calls GitHub. Carries `generated_at`, `source` (`cache` or `fresh`), `cache_ttl_s`, an `ETag` and `Cache-Control: private, max-age=`; `If-None-Match` yields 304. Each hit is recorded under REST caller `dashboard:stage-tracker` so `/api/gh-rate-limits` shows it charged nothing. | `pkg/dashboard/api.go:185` | | `POST` | `/api/prs/{owner}/{repo}/{number}/queue-automerge` | Merger/owner role | Queue PRAuto Merge | `pkg/dashboard/api.go:186` | | `GET` | `/api/inference/models/{backend}` | Dashboard auth/session | Inference Models | `pkg/dashboard/api.go:359` | | `GET` | `/api/hive-id` | Dashboard auth/session | Hive IDGet | `pkg/dashboard/api.go:421` | diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 3a9a41f8f0..cdc130fc61 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -702,7 +702,7 @@ Fed by `GET /api/stage-tracker`. Every issue and pull request follows the same p **Row actions (right-click, ⋯, Shift+F10).** Right-clicking a row — or a nested PR sub-row — opens an in-app context menu of the actions that fit that item's current state; the ⋯ button in the row's left gutter (every nested PR sub-row has its own gutter with its own checkbox and ⋯) opens the same menu for pointer-free use, and so do **Shift+F10** and the context-menu key while the row or anything in it has focus (every row is a tab stop). The menu is positioned at the pointer, clamped to the viewport, uses `menu` / `menuitem` roles, moves focus with ↑ ↓ Home End, opens the *Decide…* group with → and closes it with ←, and closes on Esc, Tab, click-away, scroll or resize, returning focus to the row. Shift+right-click keeps the browser's own menu. Entries, each shown only when it applies: **Decide…** on a row waiting on a person — a PR wearing the *Needs human decision* pill at 085 on the Merge gate column (the primary case: e.g. two competing `dibs` PRs), an issue row whose one nested PR is parked there (the menu reads *Decide PR #458…* and acts on that PR, since the issue's own stage is only mirrored from it), or an issue parked at 045. **PR-scoped actions on nested PRs** ([#15009](https://github.com/hivecommons/hive/issues/15009): one issue, two PRs — [#15017](https://github.com/hivecommons/hive/pull/15017) *Ready for human merge · untrusted author* and [#15020](https://github.com/hivecommons/hive/pull/15020) *Needs human decision* — and no way to decide one without the other): every nested PR sub-row carries its own ⋯ menu, and its *Decide…* (Let hive decide, Approve, Close as superseded by…, Won't fix…, Give direction…), *Hold* / *Release hold* and the rest act on **that PR** — `runRowAction(action, row)` receives the PR's own row object (number, repo, head), never the parent issue's, and the needs-human popover opened from a sub-row's chevron does the same. A PR waiting for a person to merge it (*Ready for human merge*, a `human_merge_reason` such as an untrusted author) counts as parked for this purpose. When an issue has two or more open nested PRs the issue row's menu no longer offers a single *Decide…* that would silently pick one: it offers **Let hive decide — all N PRs** (hint *per PR*), which asks the server to decide each PR on its own (see [Let hive decide](#let-hive-decide)); the issue's own *Decide…* stays only when the issue itself is parked at 045. *Close as superseded by…* on a nested PR with exactly one open sibling under the same issue pre-fills that sibling's number. The first entry is always *Let hive decide* (see [Let hive decide](#let-hive-decide) below): the hive decides as the owner's delegate and records it the way a person's decision is recorded. For a PR: *Approve (/lgtm + /approve)* posts those two commands as comments attributed to you and records your approval against the PR's current head as the human answer the row was parked for — a `requires_human` / decision-requested reviewer verdict, the hive's own `needs-human` park (a supersession question, say), the `sentinel-alert` label and the evidence bundle's sentinel findings are all released by it (the response's `released` lists what was: `approval-recorded`, `needs-human`, `sentinel-label`, `sentinel-acknowledged`), the stage engine re-derives the stage on the spot and the tracker cache is dropped, so the row leaves *Needs human decision* for 080 *Awaiting merge admission* on the next read; the merge gate (and the auto-merge lanes, which honour the recorded approval on that head exactly as a trusted merger's GitHub Approve review) takes it from there; the dashboard never reviews as you and never merges. A new head carries no answer: pushing to the PR re-asks. The delegated *Let hive decide* path still posts the comments only — it recommends and never clears a sentinel alert — *Close as superseded by…* (a picker listing the other open PRs in the repo, or type `#458` / `owner/repo#458` / a URL; then an optional comment, then the sweep's labelled close), *Won't fix…* (a comment naming you, then a not-planned close) and *Give direction…* (your text is recorded as `/hive decision ` against the head, so the author agent pushes a new head; only on PRs the hive opened). For an issue: *Approve direction* (records the same `/hive approve` a maintainer would comment: the parking labels come off, `approved-direction` goes on, the stage engine logs the un-park and a reply comment names you; nothing is merged), *Send back to the hive…* (`/hive decision `), *Close as superseded by…* and *Close as won't-fix…*. A selection mixing parked issues and parked PRs has Decide… greyed; **Hold** or **Release hold** depending on the `hold` chip (the same `…/items/{number}/hold` toggle as the repository cards, and the same permission — owner, or a signed-in user with write access to the repo); **Re-evaluate now** (the hive-wide read-only rescan behind the Overview's refresh — a rescan already running is reported, not duplicated); **Accept triage** (only on an issue the chevron calls *Triage needed* — stage 015, or the `needs-triage` chip on another stage — never on a PR; the one-click form of a maintainer accepting triage on GitHub: adds the reporter-trust triage label, `triage/accepted` by default or the first of `issue_filter.reporter_trust.untrusted_require_labels`, removes the awaiting label `needs-triage` / `reporter_trust.awaiting_label` and a Hive-written `stage/015-triage-needed`; the row re-reads *Actionable* with the 👤 gone at once, the reporter-trust gate admits the issue on its next poll and the stage engine records 015 → 010; same permission as Hold, audited as `repo_item_accept_triage`, a no-op when already accepted); **Mark needs-human…** (hidden once the chip is on; merger or owner role, applies `needs-human` and a marker comment through the review pipeline's send-to-human endpoint); **Open on GitHub**; **Copy link**. Entries you may not use are greyed with the reason in their tooltip, never hidden. Confirmation and free-text input use the dashboard's own dialogs — no browser prompts. Every action is an existing audited dashboard endpoint (the decision endpoint is `POST /api/repos/{owner}/{repo}/items/{number}/decision` and triage acceptance is `POST /api/repos/{owner}/{repo}/items/{number}/accept-triage`, see [api-reference.md](api-reference.md)); none of them bypasses the hive's governance or merges anything. -**What a row tells you without a click (`tracker-cues`).** The owner's framing (2026-10-28): "Hive is doing the right things mostly; we now need to keep the user informed of it — like a video game they can watch and see what happens easily." Every row therefore reads, in this order, *where it is → whether work is happening on it → what it waits on → what just happened*, with at most three cue types drawing the eye and nothing else blinking. **Where it is** is the lit chevron (the state pill). **Working** — the chevron's icon turns slowly and the pill pulses only while a hive agent is actually on the item right now (the status payload's `agents[].lastAction` for the current round, matched to the row or to the PR shown on its row); its tooltip then leads with *Working now: scout commented · 2m ago —* before the stage words. A still chevron means **waiting**; its tooltip already says where it stands and whose move it is (*Needs decision — … · Next move: human*). **Acting** — from the moment you choose a row action the affected row (and the nested PR sub-row when the action targets it) draws faint stripes over its lit chevron and an *Approving… / Hive is deciding… / Holding…* caption with a small spinner; it clears when the tracker reports the target in a new state, at once on a no-op or an error, and never later than three minutes. **Event line** — one short plain sentence under the strip about the latest thing that happened to the row, from the difference between two tracker payloads (the poll / SSE refetch the tracker already makes; no extra requests): *Hive: moved to CI / Review · 12s ago*, *Hive: fell back to Claimed — a reviewer requested changes*, *Hive: waiting on a person*, *Hive: hold released*, *You: approved*. It lives two minutes, dims for its last third, and a row carries only its latest line. **Set back** — a row wearing `regressed` colours the line's lead red. **Merged** — see *Merged today* below. The CSS class names are shared so the sibling Activity Monitor PRs (needs-human options, decision trail) compose on them: `cue-working`, `cue-acting`, `cue-event`, `cue-setback`, `cue-merged`, `cue-decision` (badge, sibling PR), `cue-spin` (the one spin keyframe), `cue-token` / `ham-merged-count` (the merge counter); the colour tokens `--cue-state`, `--cue-work`, `--cue-decision` on `.ham-row` are declared in the *Cue tokens* paragraph under [Let hive decide](#let-hive-decide) below. Legend, in the operator's words: **working** (icon turns), **waiting on X** (still; tooltip names X), **set back by Y** (red lead on the event line), **merged** (✓ glows, token rises to the count). Under `prefers-reduced-motion: reduce` every cue animation stops; the words stay. +**What a row tells you without a click (`tracker-cues`).** The owner's framing (2026-10-28): "Hive is doing the right things mostly; we now need to keep the user informed of it — like a video game they can watch and see what happens easily." Every row therefore reads, in this order, *where it is → whether work is happening on it → what it waits on → what just happened*, with at most three cue types drawing the eye and nothing else blinking. **Where it is** is the lit chevron (the state pill). **Working** — the chevron's icon turns slowly and the pill pulses only while a hive agent is actually on the item right now (the status payload's `agents[].lastAction` for the current round, matched to the row or to the PR shown on its row); its tooltip then leads with *Working now: scout commented · 2m ago —* before the stage words. A still chevron means **waiting**; its tooltip already says where it stands and whose move it is (*Needs decision — … · Next move: human*). **Acting** — from the moment you choose a row action the affected row (and the nested PR sub-row when the action targets it) draws faint stripes over its lit chevron and an *Approving… / Hive is deciding… / Holding…* caption with a small spinner; it clears when the tracker reports the target in a new state, at once on a no-op or an error, and never later than three minutes. **Event line** — one short plain sentence under the strip about the latest thing that happened to the row, from the difference between two tracker payloads (the poll / SSE refetch the tracker already makes; no extra requests): *Hive: moved to CI / Review · 12s ago*, *Hive: fell back to Claimed — a reviewer requested changes*, *Hive: waiting on a person*, *Hive: hold released*, *You: approved*. It lives two minutes, dims for its last third, and a row carries only its latest line. **Set back** — a row wearing `regressed` colours the line's lead red. **Merged** — see *Merged (the chip, the ledger, the flight)* below. The CSS class names are shared so the sibling Activity Monitor PRs (needs-human options, decision trail) compose on them: `cue-working`, `cue-acting`, `cue-event`, `cue-setback`, `cue-merged`, `cue-decision` (badge, sibling PR), `cue-spin` (the one spin keyframe), `cue-flight` / `ham-merged-count` (the glyph and the Merged chip); the colour tokens `--cue-state`, `--cue-work`, `--cue-decision` on `.ham-row` are declared in the *Cue tokens* paragraph under [Let hive decide](#let-hive-decide) below. Legend, in the operator's words: **working** (icon turns), **waiting on X** (still; tooltip names X), **set back by Y** (red lead on the event line), **merged** (✓ glows, a bee rises to the Merged chip or off the top of the screen). Under `prefers-reduced-motion: reduce` every cue animation stops; the words stay. **Result toasts (`runRowAction`).** Every row action — *Let hive decide*, *Approve*, *Give direction*, *Close as superseded*, *Won't fix*, *Approve direction*, *Send back to the hive*, *Accept triage*, *Hold* / *Release hold*, *Re-evaluate now*, *Mark needs-human* — goes through one SPA helper, `runRowAction(action, row)`, which marks the row acting, sends the request, and renders a toast from the server's shared `result` object (additive on every endpoint's existing response: `{action, target: {repo, kind, number, url}, outcome, next, rule?, applied_at?}`, `action` one of `adopted | approved | recommended | closed | directed | refused | held | released | rescanned | triage-accepted | needs-human | noop-already-applied`). The toast reads, in order, *what happened → on what → what happens next*: a headline naming the clicked row and, when it differs, the target (*Hive decided on acme/console#14938 → PR #14947: adopted (contributor offline)*), the outcome sentence, the ⚠ sentinel line when the head carries one, the *next* line (*The hive rebases and merges it unattended.*), an explicit ✕ and an *Open PR #14947 ↗* link. It does **not** auto-dismiss: it stays until the tracker reports the target row in a new stage, caption or chip set (or gone), and up to five such toasts stack for pending decisions. A no-op says so plainly — *No change — hive already recorded this decision at 12:01Z; still blocked by needs-human* — and clears the acting state at once; an error toast says what failed and why (*Hold failed on acme/console#7 — write permission required*) and is sticky but not tied to a row. Older hives that answer without `result` still get a toast built from the legacy fields. @@ -712,7 +712,7 @@ Fed by `GET /api/stage-tracker`. Every issue and pull request follows the same p **One row per PR.** A pull request that closes several open issues (`Closes #A` and `Closes #B`) is drawn once, under the issue the server names in each linked entry's `drawn_under` (its `nested_under`, the lowest-numbered open issue it closes). The other issues keep their own strip and show a compact **↳ #N · under #M** chip beside the title (tooltip: *PR #N covers this issue too; its row is drawn under #M*) instead of a second identical sub-row, so hivecommons/hive#14921 appears once under #14910 and #14911 points at it. Payloads without `drawn_under` draw as before. -**Merged today.** The column header carries, directly above the **Merge gate** and ✓ columns (right-aligned over the gate, never in the filter chip bar, so it stays put however the toolbar wraps), a small hex glyph with **Merged today: N** — pull requests the hive saw merge since your local midnight (seeded after a reload from the tracker payload's `merged_24h` list, the audit log's `pr_merged` entries over the last 24 hours, and grown live by the same merge detector that plays the zap); hovering lists the last five with their times. When a PR on the list merges its row does **not** simply vanish on the next refresh: it stays pinned for about 2.5 s, its ✓ cell glows amber, the event line reads *Hive: merged #14947 · by hive*, a flat brand-amber hex token lifts out of the ✓ cell and arcs (≈0.5 s) straight up the gate column to the counter above it — into the hive — which ticks up with a soft scale-pop — the merge zap, when sound is on, plays as the token lands — and then the row takes its normal exit. Several merges in one refresh stagger 200 ms apart with at most three tokens in flight (the rest tick the count without flying); a merged PR that is filtered out or on another page still counts. Under reduced motion nothing flies: the count ticks and the row glows. A PR drawn inline on its issue's row (the usual case — one covering PR, no sub-row) celebrates on that issue row: every row stamps the PR keys it draws (`data-ham-prs`) and the SPA remembers each PR's host row from the last payload, so the flight has somewhere to start from even when the merge also closes the issue. The ⚙ dialog's **Merges** section has **Celebrate merges** (default on, `hive.tracker.celebrate`); off, the row leaves as before and the zap plays at once. **Test celebration** beside it plays one flight from the first row of the list to the counter (pop and zap, the count unchanged) so the effect can be checked without waiting for a merge; under reduced motion it says so and only pops. The row effect is reusable in the SPA as `celebrateRow(rowEl, {kind: 'merged' | 'closed-by-merge'})`; the counter is one DOM node (`#activity-monitor-merged`) that the renderer mounts into the header slot after each refresh and parks in its toolbar home while the board is empty, so the token always targets wherever it currently sits. +**Merged (the chip, the ledger, the flight).** A thin bar at the top of the tracker — it stays pinned under the card header while the list scrolls, with the stage column header strip pinned just below it — carries a small hex glyph with **Merged N**: every pull request the hive tracked that reached merged, *all time*, with *N today · N this week* beneath. The number is the server's, not the tab's: the hive keeps a **merged ledger** at `/data/merged-ledger.json` (entries for 90 days, an all-time total that only grows, atomic writes, seeded on first boot from the audit log's `pr_merged` history with the PR throughput counter as the floor) fed by the audit log's `pr_merged` lines — the hive's own merges on every path and the poller's observations of merges by people or other bots — and by webhook / closed-sweep snapshots of a PR GitHub reports merged, which carry the title and the merger's login within seconds. `GET /api/stage-tracker` returns it as `merged{today, total_7d, total_30d, all_time, recent[≤20], tz, day_start}`; "today" rolls at midnight in the spoke's zone (`TZ`, else UTC) and the chip hydrates from it on the first render, so a reload, a new tab or a roll shows the same count (hivecommons/hive#15147 — the operator watched 4 become 0). Within a tab the number never goes down; a merge the tab just watched is added on top until the server's list carries it. **Click the chip** for a popover of the recent merges — number (linked), title, *by hive* / *by @login*, lane, time — and the 30-day total; it is also where a merged row's trace lives once the row has left. When a PR on the list merges its row does **not** simply vanish on the next refresh: it stays pinned for about 2.5 s, its ✓ cell glows amber, the event line reads *Hive: merged #14947 · by hive*, and — in the operator's words — "an animation originates from the item's row where the check mark is and flies up to the top of the browser border; if the Merged pill is visible the animation stops there, if not we just see it go off the screen": a 🐝 (or **+1**, ⚙ → Merges → *Glyph*, `hive.tracker.celebrate_glyph`) rises out of the ✓ cell for ≈0.9 s with a slight arc, lands on the chip when the chip's box is inside the viewport (the count bumps with a soft scale-pop and the merge zap, when sound is on, plays as it lands) or exits through the top edge otherwise (the count still bumps). It works at any scroll position (`position: fixed`, `pointer-events: none`). At the same moment a **banner** appears at the bottom right — *✓ #15067 merged — fix: enable bulk delegation… (adopted, by hive, 17:42)* — and stays until dismissed or 30 s, stacked (newest at the bottom, at most five) when several merge together, with **Show row** (scrolls to the row and glows it again; once the row has left, says so and opens the list), **Open on GitHub** and **All merges**. Several merges in one refresh stagger 200 ms apart with at most three glyphs in flight (the rest bump the count without flying); a merged PR that is filtered out or on another page still counts and still gets its banner. Under reduced motion nothing flies: the glyph fades in place, the count bumps, the row glows, the banners appear without sliding. A PR drawn inline on its issue's row (the usual case — one covering PR, no sub-row) celebrates on that issue row: every row stamps the PR keys it draws (`data-ham-prs`) and the SPA remembers each PR's host row from the last payload, so the flight has somewhere to start from even when the merge also closes the issue. The ⚙ dialog's **Merges** section has **Celebrate merges** (default on, `hive.tracker.celebrate`); off, the row leaves as before and the zap plays at once. **Test celebration** beside it closes the dialog, picks the first row currently in the viewport (scrolling one into view if none is) and plays one flight from it with a *(test)* banner — pop and zap, the count itself unchanged — so the effect can be checked without waiting for a merge; under reduced motion it says so and only pops. The row effect is reusable in the SPA as `celebrateRow(rowEl, {kind: 'merged' | 'closed-by-merge'})`. Class names: `ham-sticky` (the pinned bar), `ham-merged-count` / `ham-merged-pop` (chip, popover), `cue-flight` / `cue-flight-fade` (the glyph), `merge-banners` / `merge-banner`. **Multi-select.** The checkbox in a row's left gutter selects the row — and the checkbox in a nested PR sub-row's gutter selects that PR on its own, so one or more PRs under an issue can be picked without the issue or each other; **Shift+click** selects the range from the last clicked row (sub-rows are in the range order right after their issue), **Cmd/Ctrl+click** toggles, **Cmd/Ctrl+Shift+click** adds a range to what is selected; with a row focused, **Space** toggles it (Shift+Space extends). Cmd/Ctrl- or Shift-clicking the row itself, away from its links, selects it too. A selection bar appears above the list with the count, **Clear**, and the same actions for the whole selection — *Decide…* (opens the group as a menu, *Let hive decide* first — each selected row gets its own delegated-decision job, see below), **Let hive decide (N selected)** (one click for the whole selection: one delegated-decision job per selected item, each nested PR sub-row by its own number, with one summary toast — "3 sent · 2 decided · 1 no-op", plus "· 1 failed" when any failed — and the per-action confirm / *Don't ask again* of *Let hive decide*), *Hold*, *Release hold*, *Re-evaluate now*, *Mark needs-human…*, *Copy links* — each enabled only when it is valid for every selected row and otherwise greyed with the first reason ("Not applicable to acme/console#12", "Sign in with write access…"). Right-clicking a selected row acts on the whole selection. Bulk actions run one request per row, in list order, under a progress toast ("Hold 2/5 — acme/console#12…"); the result toast lists each failure by row and the successes stand, after which the list and the status snapshot refresh (a hive-wide rescan is sent once per batch). Selection is keyed on the row's item (repository, kind, number), not its node, so the live re-render keeps it; a row that leaves the list — merged, closed, filtered or paged out — leaves the selection. diff --git a/src/pkg/dashboard/merged_ledger.go b/src/pkg/dashboard/merged_ledger.go index 6d29648ff4..4dcf32de18 100644 --- a/src/pkg/dashboard/merged_ledger.go +++ b/src/pkg/dashboard/merged_ledger.go @@ -16,7 +16,7 @@ import ( // The merged ledger is the Activity Monitor's server-side record of every // pull request the hive tracked that reached merged — whoever merged it -// (hivecommons/hive#15096). Before it, the toolbar's "Merged today" chip was +// (hivecommons/hive#15147, #15096). Before it, the toolbar's "Merged today" chip was // accumulated in the tab from rows vanishing as merged and seeded from the // in-memory audit ring on reload, so a refresh, a new tab or a roll of the // pod reset it (the operator watched 4 become 0, and 33 become 4). diff --git a/src/pkg/dashboard/merged_ledger_test.go b/src/pkg/dashboard/merged_ledger_test.go new file mode 100644 index 0000000000..2596036761 --- /dev/null +++ b/src/pkg/dashboard/merged_ledger_test.go @@ -0,0 +1,364 @@ +package dashboard + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" +) + +// Fixed instants for the ledger tests: a Tuesday afternoon in UTC, which is +// still Monday evening in Los Angeles — so "today" differs by zone. +var ( + mergedTestNow = time.Date(2026, 10, 27, 2, 30, 0, 0, time.UTC) + mergedTestLA = mustLoadLocation("America/Los_Angeles") +) + +func mustLoadLocation(name string) *time.Location { + loc, err := time.LoadLocation(name) + if err != nil { + panic(err) + } + return loc +} + +func mergedEntry(repo string, number int, at time.Time) MergedLedgerEntry { + return MergedLedgerEntry{Repo: repo, Number: number, MergedAt: at} +} + +// isolateMergedLedger points the server's ledger at a file under a temp +// dir for the test's lifetime. +func isolateMergedLedger(t *testing.T) string { + t.Helper() + old := mergedLedgerPath + mergedLedgerPath = filepath.Join(t.TempDir(), "merged-ledger.json") + t.Cleanup(func() { mergedLedgerPath = old }) + return mergedLedgerPath +} + +// TestMergedLedgerPersistsAcrossReload: the file under the ledger path +// survives a new constructor (a roll), carrying the all-time total and the +// entries; a ledger that found a file reports Seeded so a boot never seeds +// twice. +func TestMergedLedgerPersistsAcrossReload(t *testing.T) { + path := filepath.Join(t.TempDir(), "merged-ledger.json") + l := newMergedLedger(path) + if l.Seeded() { + t.Fatal("a fresh ledger with no file must not report seeded") + } + if !l.Record(MergedLedgerEntry{Repo: "acme/console", Number: 1, Title: "fix: one", MergedBy: MergedByHive, Lane: "hive"}, mergedTestNow) { + t.Fatal("first record must be new") + } + if l.Record(mergedEntry("acme/console", 2, mergedTestNow.Add(-time.Hour)), mergedTestNow) != true { + t.Fatal("second PR is new") + } + if l.Record(mergedEntry("acme/console", 1, mergedTestNow), mergedTestNow) { + t.Fatal("the same PR again is not new") + } + + again := newMergedLedger(path) + if !again.Seeded() { + t.Fatal("a ledger loaded from disk reports seeded") + } + sum := again.Summary(mergedTestNow, time.UTC) + if sum.AllTime != 2 || again.Len() != 2 { + t.Fatalf("after reload: all_time=%d len=%d", sum.AllTime, again.Len()) + } + if len(sum.Recent) != 2 || sum.Recent[0].Number != 1 || sum.Recent[0].Title != "fix: one" || sum.Recent[0].MergedBy != MergedByHive || sum.Recent[0].Lane != "hive" { + t.Fatalf("recent after reload: %+v", sum.Recent) + } + if sum.Recent[0].MergedAt.IsZero() { + t.Fatal("a record with no merge time takes now") + } +} + +// TestMergedLedgerUpsertFillsBlanks: a second record of the same PR fills in +// what the first did not know (title, merger, lane, issue), never overwrites +// a known field, and the earliest merge time wins. +func TestMergedLedgerUpsertFillsBlanks(t *testing.T) { + l := newMergedLedger("") + l.Record(MergedLedgerEntry{Repo: "acme/console", Number: 5, MergedAt: mergedTestNow.Add(-2 * time.Minute), MergedBy: "octocat"}, mergedTestNow) + l.Record(MergedLedgerEntry{Repo: " acme/console ", Number: 5, MergedAt: mergedTestNow.Add(-10 * time.Minute), Title: "feat: five", MergedBy: MergedByHive, Lane: "contributor", Issue: 42}, mergedTestNow) + sum := l.Summary(mergedTestNow, time.UTC) + if sum.AllTime != 1 || len(sum.Recent) != 1 { + t.Fatalf("one PR, two records: all_time=%d recent=%d", sum.AllTime, len(sum.Recent)) + } + e := sum.Recent[0] + if e.Title != "feat: five" || e.Lane != "contributor" || e.Issue != 42 { + t.Fatalf("blanks not filled: %+v", e) + } + if e.MergedBy != "octocat" { + t.Fatalf("a known merger must not be overwritten: %q", e.MergedBy) + } + if !e.MergedAt.Equal(mergedTestNow.Add(-10 * time.Minute)) { + t.Fatalf("earliest merge time wins: %v", e.MergedAt) + } +} + +// TestMergedLedgerRetentionAndCap: entries older than the retention window +// are pruned, the oldest beyond the cap are dropped, and the all-time total +// is untouched by either. +func TestMergedLedgerRetentionAndCap(t *testing.T) { + l := newMergedLedger("") + l.Record(mergedEntry("acme/console", 1, mergedTestNow.Add(-mergedLedgerRetention-time.Hour)), mergedTestNow) + l.Record(mergedEntry("acme/console", 2, mergedTestNow.Add(-29*24*time.Hour)), mergedTestNow) + l.Record(mergedEntry("acme/console", 3, mergedTestNow.Add(-time.Hour)), mergedTestNow) + sum := l.Summary(mergedTestNow, time.UTC) + if sum.AllTime != 3 { + t.Fatalf("all_time counts the pruned one: %d", sum.AllTime) + } + if l.Len() != 2 || len(sum.Recent) != 2 || sum.Recent[0].Number != 3 || sum.Recent[1].Number != 2 { + t.Fatalf("retention: len=%d recent=%+v", l.Len(), sum.Recent) + } + if sum.Total30d != 2 || sum.Total7d != 1 { + t.Fatalf("windows: 30d=%d 7d=%d", sum.Total30d, sum.Total7d) + } + + capped := newMergedLedger("") + for i := 1; i <= mergedLedgerMaxEntries+3; i++ { + capped.Record(mergedEntry("acme/console", i, mergedTestNow.Add(-time.Duration(mergedLedgerMaxEntries+3-i)*time.Second)), mergedTestNow) + } + if capped.Len() != mergedLedgerMaxEntries { + t.Fatalf("cap: len=%d", capped.Len()) + } + capSum := capped.Summary(mergedTestNow, time.UTC) + if capSum.AllTime != mergedLedgerMaxEntries+3 { + t.Fatalf("cap leaves all_time alone: %d", capSum.AllTime) + } + if len(capSum.Recent) != mergedLedgerRecentMax || capSum.Recent[0].Number != mergedLedgerMaxEntries+3 { + t.Fatalf("recent is the newest %d: len=%d first=%d", mergedLedgerRecentMax, len(capSum.Recent), capSum.Recent[0].Number) + } +} + +// TestMergedLedgerSummaryTodayRollsInZone: "today" starts at midnight in +// the given zone — the same instants count differently in UTC and in Los +// Angeles — and the day_start / tz fields say which day the number is for. +// nil means UTC. Entries stamped in the future are left out. +func TestMergedLedgerSummaryTodayRollsInZone(t *testing.T) { + l := newMergedLedger("") + // 2026-10-27 01:00Z: today in UTC, still Monday the 26th in LA. + l.Record(mergedEntry("acme/console", 1, mergedTestNow.Add(-90*time.Minute)), mergedTestNow) + // 2026-10-26 23:00Z: yesterday in UTC, Monday afternoon in LA. + l.Record(mergedEntry("acme/console", 2, mergedTestNow.Add(-210*time.Minute)), mergedTestNow) + // 2026-10-26 06:00Z: Sunday night in LA. + l.Record(mergedEntry("acme/console", 3, mergedTestNow.Add(-20*time.Hour-30*time.Minute)), mergedTestNow) + // Stamped an hour ahead: not counted anywhere. + l.Record(mergedEntry("acme/console", 4, mergedTestNow.Add(time.Hour)), mergedTestNow) + + utc := l.Summary(mergedTestNow, nil) + if utc.TZ != "UTC" || !utc.DayStart.Equal(time.Date(2026, 10, 27, 0, 0, 0, 0, time.UTC)) { + t.Fatalf("utc zone: tz=%q day_start=%v", utc.TZ, utc.DayStart) + } + if utc.Today != 1 || utc.Total7d != 3 || utc.Total30d != 3 || utc.AllTime != 4 { + t.Fatalf("utc: %+v", utc) + } + if len(utc.Recent) != 3 || utc.Recent[0].Number != 1 { + t.Fatalf("utc recent (newest first, no future): %+v", utc.Recent) + } + + la := l.Summary(mergedTestNow, mergedTestLA) + if la.TZ != "America/Los_Angeles" || !la.DayStart.Equal(time.Date(2026, 10, 26, 0, 0, 0, 0, mergedTestLA)) { + t.Fatalf("la zone: tz=%q day_start=%v", la.TZ, la.DayStart) + } + if la.Today != 2 { + t.Fatalf("la today: %d (Monday the 26th has #1 and #2)", la.Today) + } + + // Midnight rolls the count: one second past LA midnight on the 27th, + // Monday's merges are yesterday's. + rolled := time.Date(2026, 10, 27, 0, 0, 1, 0, mergedTestLA) + l.Record(mergedEntry("acme/console", 9, rolled.Add(-time.Second)), rolled) + after := l.Summary(rolled, mergedTestLA) + if after.Today != 1 || after.Recent[0].Number != 9 { + t.Fatalf("after midnight: today=%d first=%d", after.Today, after.Recent[0].Number) + } +} + +// TestMergedLedgerSeedFloorAndOnce: the first boot folds history in, +// deduplicated, with the throughput counter as the all-time floor; the +// ledger then reports seeded and a later seed is not needed. +func TestMergedLedgerSeedFloorAndOnce(t *testing.T) { + path := filepath.Join(t.TempDir(), "merged-ledger.json") + l := newMergedLedger(path) + l.Seed([]MergedLedgerEntry{ + mergedEntry("acme/console", 1, mergedTestNow.Add(-time.Hour)), + mergedEntry("acme/console", 1, mergedTestNow.Add(-time.Hour)), + mergedEntry("acme/console", 2, mergedTestNow.Add(-2*time.Hour)), + {Repo: "", Number: 3, MergedAt: mergedTestNow}, + }, 33, mergedTestNow) + sum := l.Summary(mergedTestNow, time.UTC) + if sum.AllTime != 33 || l.Len() != 2 { + t.Fatalf("seed: all_time=%d (floor 33) len=%d", sum.AllTime, l.Len()) + } + if !l.Seeded() { + t.Fatal("seeded") + } + if !newMergedLedger(path).Seeded() { + t.Fatal("the seed persisted") + } + // A seed smaller than what is already known keeps the larger count. + l2 := newMergedLedger("") + l2.Seed([]MergedLedgerEntry{mergedEntry("a/b", 1, mergedTestNow), mergedEntry("a/b", 2, mergedTestNow)}, 1, mergedTestNow) + if got := l2.Summary(mergedTestNow, time.UTC).AllTime; got != 2 { + t.Fatalf("floor below the entries: %d", got) + } +} + +// TestMergedEntryFromAudit: pr_merged audit lines project onto the ledger +// — the hive's own paths are MergedByHive, an observed merge names its +// actor, "unknown" is blank; short repo names are qualified; other actions +// and lines without a PR are rejected. +func TestMergedEntryFromAudit(t *testing.T) { + qualify := func(r string) string { + if r == "console" { + return "acme/console" + } + return r + } + ts := mergedTestNow.Format(time.RFC3339) + cases := []struct { + name string + e AuditEntry + ok bool + repo string + number int + by string + }{ + {"sweep", AuditEntry{Timestamp: ts, Action: github.AuditActionPRMerged, Repo: "console", Target: 7, Detail: "path=" + github.PRAuditPathSweep}, true, "acme/console", 7, MergedByHive}, + {"observed human", AuditEntry{Timestamp: ts, Action: github.AuditActionPRMerged, Repo: "acme/console", Target: 8, Detail: "path=observed, actor=octocat"}, true, "acme/console", 8, "octocat"}, + {"observed unknown", AuditEntry{Timestamp: ts, Action: github.AuditActionPRMerged, Repo: "acme/console", Target: 9, Detail: "path=observed, actor=unknown"}, true, "acme/console", 9, ""}, + {"legacy detail", AuditEntry{Timestamp: ts, Action: github.AuditActionPRMerged, Detail: "repo=console, number=10, path=queue"}, true, "acme/console", 10, MergedByHive}, + {"other action", AuditEntry{Timestamp: ts, Action: "pr_approved", Repo: "acme/console", Target: 11}, false, "", 0, ""}, + {"no pr", AuditEntry{Timestamp: ts, Action: github.AuditActionPRMerged, Repo: "acme/console"}, false, "", 0, ""}, + {"bad time", AuditEntry{Timestamp: "yesterday", Action: github.AuditActionPRMerged, Repo: "acme/console", Target: 12}, false, "", 0, ""}, + } + for _, c := range cases { + got, ok := mergedEntryFromAudit(c.e, qualify) + if ok != c.ok { + t.Fatalf("%s: ok=%v want %v", c.name, ok, c.ok) + } + if !ok { + continue + } + if got.Repo != c.repo || got.Number != c.number || got.MergedBy != c.by || !got.MergedAt.Equal(mergedTestNow) { + t.Fatalf("%s: %+v", c.name, got) + } + } +} + +// TestMergedLedgerServerRecordsAuditAndSnapshots: on the server the audit +// log's pr_merged lines and webhook snapshots of merged PRs both land in +// the ledger (deduplicated), a non-merged or non-PR snapshot does not, and +// the tracker cache is dropped so the next /api/stage-tracker shows it. +func TestMergedLedgerServerRecordsAuditAndSnapshots(t *testing.T) { + isolateMergedLedger(t) + s := newTestServer() + s.deps = testDeps(t) + l := s.mergedLedger() + if l == nil || l.Summary(mergedTestNow, nil).AllTime != 0 { + t.Fatal("a fresh server starts an empty ledger") + } + + s.audit.LogRecord("system", github.AuditActionPRMerged, "path="+github.PRAuditPathQueue+", number=21", "governor", "acme/console", 21) + s.recordMergedSnapshots([]github.WebhookItemSnapshot{ + {Repo: "acme/console", Number: 21, IsPR: true, Merged: true, Title: "fix: twenty-one", URL: "https://github.com/acme/console/pull/21", MergedAt: mergedTestNow, MergedBy: "hive-bot[bot]", MergedByHive: true}, + {Repo: "acme/console", Number: 22, IsPR: true, Merged: true, Title: "feat: twenty-two", MergedBy: "octocat", UpdatedAt: mergedTestNow}, + {Repo: "acme/console", Number: 23, IsPR: true, Merged: false, Title: "open"}, + {Repo: "acme/console", Number: 24, IsPR: false, Merged: true, Title: "an issue"}, + }) + sum := l.Summary(time.Now(), nil) + if sum.AllTime != 2 || len(sum.Recent) != 2 { + t.Fatalf("two merged PRs: all_time=%d recent=%+v", sum.AllTime, sum.Recent) + } + byNum := map[int]MergedLedgerEntry{} + for _, e := range sum.Recent { + byNum[e.Number] = e + } + if e := byNum[21]; e.Title != "fix: twenty-one" || e.URL == "" || e.MergedBy != MergedByHive { + t.Fatalf("#21 audit then snapshot, blanks filled: %+v", e) + } + if e := byNum[22]; e.MergedBy != "octocat" || !e.MergedAt.Equal(mergedTestNow) { + t.Fatalf("#22 human merge, updated_at as the merge time: %+v", e) + } + + // Survives a roll: a new server over the same path sees the two and + // does not seed on top of them. + s2 := newTestServer() + s2.deps = testDeps(t) + if got := s2.mergedLedger().Summary(time.Now(), nil).AllTime; got != 2 { + t.Fatalf("after roll: all_time=%d", got) + } +} + +// TestStageTrackerAPIMergedShape: /api/stage-tracker carries the merged +// block — today, total_7d, total_30d, all_time, recent (newest first, with +// the fields the chip's popover draws), tz and day_start — and a merge +// recorded between two requests shows in the next response. +func TestStageTrackerAPIMergedShape(t *testing.T) { + now := time.Date(2026, 10, 17, 9, 0, 0, 0, time.UTC) + s, _ := stageTrackerFixture(t, now) + deps := testDeps(t) + gh := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "unexpected", http.StatusTeapot) + })) + t.Cleanup(gh.Close) + deps.GHClient = github.NewClientForTest(gh.URL, "acme", []string{"console"}, covBLogger()) + s.deps = deps + isolateMergedLedger(t) + + get := func() map[string]json.RawMessage { + req := httptest.NewRequest(http.MethodGet, "/api/stage-tracker", nil) + rr := httptest.NewRecorder() + s.handleStageTracker(rr, req) + if rr.Code != http.StatusOK { + t.Fatalf("status %d: %s", rr.Code, rr.Body.String()) + } + var body map[string]json.RawMessage + if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil { + t.Fatal(err) + } + return body + } + var merged struct { + Today int `json:"today"` + Total7d int `json:"total_7d"` + Total30d int `json:"total_30d"` + AllTime int `json:"all_time"` + Recent []MergedLedgerEntry `json:"recent"` + TZ string `json:"tz"` + DayStart time.Time `json:"day_start"` + } + raw, ok := get()["merged"] + if !ok { + t.Fatal("response has no merged block") + } + if err := json.Unmarshal(raw, &merged); err != nil { + t.Fatal(err) + } + if merged.AllTime != 0 || merged.Recent == nil || len(merged.Recent) != 0 || merged.TZ == "" || merged.DayStart.IsZero() { + t.Fatalf("empty ledger block: %s", raw) + } + for _, k := range []string{"today", "total_7d", "total_30d", "all_time", "recent", "tz", "day_start"} { + var m map[string]json.RawMessage + _ = json.Unmarshal(raw, &m) + if _, ok := m[k]; !ok { + t.Fatalf("merged block lacks %q: %s", k, raw) + } + } + + s.recordMergedSnapshots([]github.WebhookItemSnapshot{{Repo: "acme/console", Number: 102, IsPR: true, Merged: true, Title: "feat: soak done", URL: "https://github.com/acme/console/pull/102", MergedAt: time.Now().Add(-time.Minute), MergedBy: "octocat"}}) + if err := json.Unmarshal(get()["merged"], &merged); err != nil { + t.Fatal(err) + } + if merged.AllTime != 1 || merged.Today != 1 || merged.Total7d != 1 || merged.Total30d != 1 || len(merged.Recent) != 1 { + t.Fatalf("after a merge: %+v", merged) + } + e := merged.Recent[0] + if e.Repo != "acme/console" || e.Number != 102 || e.Title != "feat: soak done" || e.MergedBy != "octocat" || e.URL == "" || e.MergedAt.IsZero() { + t.Fatalf("recent entry: %+v", e) + } +} diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 99ec6d87c2..f7f67b8755 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -87,10 +87,10 @@ type StageTrackerResponse struct { // the SPA tell "merged" from "closed" between two polls and play its // opt-in merge sound for the right rows. RecentlyMerged []StageTrackerMerged `json:"recently_merged,omitempty"` - // Merged24h is the same list over the last 24 hours, newest first, so - // the SPA's "Merged today" counter is right after a reload (it counts - // the entries since the viewer's local midnight). Best effort: the - // in-memory audit ring is the only source, as for RecentlyMerged. + // Merged24h is the same list over the last 24 hours, newest first. It + // seeded the SPA's former "Merged today" counter; the Merged chip now + // reads Merged (the durable ledger) and this stays for older SPAs. + // Best effort: the in-memory audit ring is the only source. Merged24h []StageTrackerMerged `json:"merged_24h,omitempty"` // Merged is the durable merge ledger's summary (merged_ledger.go): // counts for today (in the spoke's zone), 7 and 30 days, all time, and diff --git a/src/pkg/dashboard/stage_tracker_actor_icon_test.go b/src/pkg/dashboard/stage_tracker_actor_icon_test.go index 2a9cf73c20..1b3460dd77 100644 --- a/src/pkg/dashboard/stage_tracker_actor_icon_test.go +++ b/src/pkg/dashboard/stage_tracker_actor_icon_test.go @@ -172,37 +172,48 @@ assert.strictEqual(rows[1], null); `) } -// The "Merged today" counter lives in the column header, over the Merge -// gate and ✓ columns (not in the filter chip bar), so a merge token's flight -// from a row's ✓ cell is short and ends above the gate. It is one node for -// the life of the page: the renderer mounts it into the header slot after -// each reconcile and parks it in its toolbar home when the board is empty. +// The Merged chip lives in a sticky bar at the top of the tracker — pinned +// under the card header while the list scrolls, with the stage column +// header strip pinned just below it — so the chip is in view whenever the +// list is (hivecommons/hive#15147; the operator never saw the celebration +// land in a toolbar 2,300 px above the viewport). It is one node for the +// life of the page, resolved through stageTrackerMergedChip() by the count +// draw, the popover and the flight alike; the column header is a single +// decorative row again (no counter slot). func TestStageTrackerMergedCounterPlacement(t *testing.T) { html := indexHTML(t) for _, want := range []string{ - ``, - `.ham-cols-top .ham-seg.ham-merged-slot-seg { flex: 1 1 calc(4em + var(--sp-1)); justify-content: flex-end;`, - `stageTrackerMergedCountMount(el);`, - `stageTrackerMergedCountMount(null);`, - `@media (prefers-reduced-motion: reduce) { .cue-token { display: none; } .ham-merged-count.cue-tick { animation: none; }`, + `
`, + `
`, + `.ham-sticky { position: sticky; top: var(--navbar-sticky-height); z-index: calc(var(--z-sticky) + 1);`, + `.ham-cols { position: sticky; top: calc(var(--navbar-sticky-height) + var(--ham-sticky-bar-h));`, + `@media (prefers-reduced-motion: reduce) { .cue-flight { animation-name: cue-flight-fade; } .ham-merged-count.cue-tick { animation: none; }`, `id="sts-celebrate-on" data-change-action="stageTrackerCelebrateChange"`, } { if !strings.Contains(html, want) { - t.Errorf("index.html missing merged-counter placement wiring %q", want) + t.Errorf("index.html missing merged-chip placement wiring %q", want) } } - for _, fn := range []string{"celebrateRow", "stageTrackerMergedCountDraw"} { + if i, j := strings.Index(html, `id="activity-monitor-sticky"`), strings.Index(html, `id="activity-monitor-panel"`); i < 0 || j < 0 || i > j { + t.Errorf("the sticky bar must precede the panel: sticky=%d panel=%d", i, j) + } + for _, gone := range []string{"ham-merged-home", "ham-merged-slot", "ham-cols-top", "stageTrackerMergedCountMount", "Merged today"} { + if strings.Contains(html, gone) { + t.Errorf("index.html still carries the retired %q", gone) + } + } + for _, fn := range []string{"celebrateRow", "stageTrackerMergedCountDraw", "hamMergedToggle"} { src := jsFunc(t, html, fn) if strings.Contains(src, "getElementById('activity-monitor-merged')") || !strings.Contains(src, "stageTrackerMergedChip()") { - t.Errorf("%s must resolve the counter through stageTrackerMergedChip() so the token targets the mounted node", fn) + t.Errorf("%s must resolve the chip through stageTrackerMergedChip() so the flight, the draw and the popover share one node", fn) } } - // The toolbar-home markup and the header slot CSS name no literal colour. + // The sticky bar and chip CSS name no literal colour. for _, line := range strings.Split(html, "\n") { - if strings.Contains(line, ".ham-merged-slot") || strings.Contains(line, ".ham-cols-top") || strings.Contains(line, ".ham-merged-home") { + if strings.Contains(line, ".ham-sticky") || strings.Contains(line, ".ham-merged-pop") { if strings.Contains(line, "#") || strings.Contains(line, "rgb(") { - t.Errorf("merged-counter CSS must use tokens only: %s", strings.TrimSpace(line)) + t.Errorf("merged-chip CSS must use tokens only: %s", strings.TrimSpace(line)) } } } @@ -214,63 +225,37 @@ func TestStageTrackerMergedCounterPlacement(t *testing.T) { jsFunc(t, html, "stageTrackerIcon"), jsFunc(t, html, "stageTrackerHeaderHtml"), jsFunc(t, html, "stageTrackerMergedChip"), - jsFunc(t, html, "stageTrackerMergedCountMount"), }, "\n") runStageTrackerRowActionsScript(t, lifted+` const assert = require('assert'); const stages = [{ number: 10, short: 'Actionable' }, { number: 20, short: 'Claimed' }, { number: 30, short: 'PR in progress' }, { number: 50, short: 'CI / Review' }, { number: 80, short: 'Merge gate' }]; const header = stageTrackerHeaderHtml(stages); -const top = /
([\s\S]*?)<\/div>/.exec(header)[1]; -const names = header.slice(header.indexOf('
') + 6); -assert.ok(header.indexOf('ham-cols-top') < header.indexOf('Merge gate'), 'the counter row sits above the column names'); -assert.ok(names.startsWith('