Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
c829e30
✨ feat: record per-contributor abandoned PRs when the hive adopts them
hivecommons-hive[bot] Oct 30, 2026
d2efc82
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
ef7c17a
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
822b2c9
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
968145c
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
a211ad1
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
7059422
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
c38457b
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
832df67
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
3a656f6
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
39ea0ac
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
e2772b0
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
eb18241
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
63ad944
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
44a77f6
Merge branch 'v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
c705d21
chore: refresh head so the merge relay re-evaluates (Sign-off check i…
hivecommons-hive[bot] Oct 30, 2026
e8705b0
Merge remote-tracking branch 'origin/v5' into scanner/fix-15782
hivecommons-hive[bot] Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-contributor-abandoned-counter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Each contributor profile now records the PRs the hive had to adopt from that contributor: `abandoned` (one per PR, deduped) plus `abandoned_prs` records `{repo, pr, adopted_into_pr, reason, at}` on `/api/contributors`, counted at in-place adoption, supersession onto `hive/adopt-<n>` and operator takeovers (Adopt now / `adoption/supersede`, reason `operator_takeover`); automatic adoptions carry `offline` or `unresponsive` ([#15782](https://github.com/hivecommons/hive/issues/15782)).
12 changes: 12 additions & 0 deletions src/cmd/hive/contributor_nudge_sink.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

import (
Expand Down Expand Up @@ -44,3 +44,15 @@
h.Resolve(ctx, owner, repo, number)
}
}

// Abandoned implements automerge.ContributorAbandonSink so every adoption
// the sweep performs lands on the author's contributor profile
// (hivecommons/hive#15782); without the forwarder the engine's type
// assertion fails on the wrapper, as it once did for Presence.
func (s lateContributorNudgeSink) Abandoned(ctx context.Context, a automerge.ContributorAbandon) {
if h := s.hub(); h != nil {
h.Abandoned(ctx, a)
}
}

var _ automerge.ContributorAbandonSink = lateContributorNudgeSink{}
110 changes: 110 additions & 0 deletions src/pkg/dashboard/contribute_abandoned.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package dashboard

import (
"context"
"errors"
"strconv"
"strings"
"time"

"github.com/hivecommons/hive/pkg/github/automerge"
)

// ContributorAbandonRecord is one PR of a contributor the hive adopted
// (hivecommons/hive#15782). AdoptedIntoPR is the hive's superseding PR, 0
// while the adoption is in place on the contributor's own branch.
type ContributorAbandonRecord struct {
Repo string `json:"repo"`
PR int `json:"pr"`
AdoptedIntoPR int `json:"adopted_into_pr"`
Reason string `json:"reason"`
At string `json:"at"`
}

func contributorAbandonKey(repo string, pr int) string {
return strings.ToLower(strings.TrimSpace(repo)) + "#" + strconv.Itoa(pr)
}

// applyContributorAbandon adds rec to p once per PR. A later adoption of an
// already-counted PR (an in-place adoption re-homed onto hive/adopt-<n>, a
// re-adoption after an orphaned copy) only fills in the superseding PR the
// first record lacked; it never counts again. changed reports whether p
// needs saving, counted whether the counter moved.
func applyContributorAbandon(p *ContributorProfile, rec ContributorAbandonRecord) (changed, counted bool) {
key := contributorAbandonKey(rec.Repo, rec.PR)
for i := range p.AbandonedPRs {
if contributorAbandonKey(p.AbandonedPRs[i].Repo, p.AbandonedPRs[i].PR) != key {
continue
}
if p.AbandonedPRs[i].AdoptedIntoPR == 0 && rec.AdoptedIntoPR > 0 {
p.AbandonedPRs[i].AdoptedIntoPR = rec.AdoptedIntoPR
return true, false
}
return false, false
}
p.AbandonedPRs = append(p.AbandonedPRs, rec)
p.Abandoned = len(p.AbandonedPRs)
return true, true
}

// mergeContributorAbandons is the save-time reconcile: every record on disk
// survives, plus the incoming ones not already there (deduped per PR).
func mergeContributorAbandons(disk, incoming []ContributorAbandonRecord) []ContributorAbandonRecord {
if len(disk) == 0 && len(incoming) == 0 {
return nil
}
merged := &ContributorProfile{AbandonedPRs: append([]ContributorAbandonRecord(nil), disk...)}
for _, rec := range incoming {
applyContributorAbandon(merged, rec)
}
return merged.AbandonedPRs
}

// recordContributorAbandon persists one adoption against the author's
// contributor profile. Authors without a profile on this hive (people,
// other hives' clankers, bots) are not recorded.
func recordContributorAbandon(a automerge.ContributorAbandon) (counted bool, err error) {
login := strings.TrimSpace(strings.TrimPrefix(a.Author, "@"))
repo := strings.TrimSpace(a.Repo)
if login == "" || repo == "" || a.PR <= 0 {
return false, nil
}
at := a.At
if at.IsZero() {
at = time.Now()
}
rec := ContributorAbandonRecord{Repo: repo, PR: a.PR, AdoptedIntoPR: a.AdoptedIntoPR, Reason: a.Reason, At: at.UTC().Format(time.RFC3339)}
for attempt := 0; attempt < 3; attempt++ {
p := findContributor(login)
if p == nil || !strings.EqualFold(p.GitHubUsername, login) {
return false, nil
}
changed, counted := applyContributorAbandon(p, rec)
if !changed {
return false, nil
}
err = saveContributorProfile(p)
if errors.Is(err, errProfileConflict) {
continue
}
return counted, err
}
return false, err
}

// Abandoned implements automerge.ContributorAbandonSink.
func (h *ContributeWSHub) Abandoned(_ context.Context, a automerge.ContributorAbandon) {
counted, err := recordContributorAbandon(a)
if h == nil || h.logger == nil {
return
}
if err != nil {
h.logger.Warn("[contribute-ws] abandoned PR not recorded", "username", a.Author, "repo", a.Repo, "pr", a.PR, "error", err)
return
}
if counted {
h.logger.Info("[contribute-ws] abandoned PR recorded", "username", a.Author, "repo", a.Repo, "pr", a.PR, "adopted_into_pr", a.AdoptedIntoPR, "reason", a.Reason)
}
}

var _ automerge.ContributorAbandonSink = (*ContributeWSHub)(nil)
115 changes: 115 additions & 0 deletions src/pkg/dashboard/contribute_abandoned_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package dashboard

import (
"context"
"encoding/json"
"net/http/httptest"
"testing"
"time"

"github.com/hivecommons/hive/pkg/github/automerge"
)

func abandonFixture(t *testing.T) {
t.Helper()
t.Setenv("HIVE_CONTRIBUTORS_DIR", t.TempDir())
if err := saveContributorProfile(&ContributorProfile{GitHubUsername: "Mallory", ContributorID: "c-mal", TrustTier: "contributor"}); err != nil {
t.Fatalf("seed: %v", err)
}
}

func TestRecordContributorAbandonDedupesPerPR(t *testing.T) {
abandonFixture(t)
at := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)

// In place first: counted once, no copy yet.
counted, err := recordContributorAbandon(automerge.ContributorAbandon{Author: "mallory", Repo: "acme/widget", PR: 7, Reason: automerge.AbandonReasonOffline, At: at})
if err != nil || !counted {
t.Fatalf("first adoption: counted=%v err=%v", counted, err)
}
// Re-homed onto hive/adopt-7: fills in the copy, does not count again.
counted, err = recordContributorAbandon(automerge.ContributorAbandon{Author: "mallory", Repo: "Acme/Widget", PR: 7, AdoptedIntoPR: 90, Reason: automerge.AbandonReasonUnresponsive, At: at.Add(time.Hour)})
if err != nil || counted {
t.Fatalf("re-home: counted=%v err=%v", counted, err)
}
// The same PR adopted again: no-op.
if counted, err = recordContributorAbandon(automerge.ContributorAbandon{Author: "mallory", Repo: "acme/widget", PR: 7, AdoptedIntoPR: 91, Reason: automerge.AbandonReasonOperatorTakeover, At: at.Add(2 * time.Hour)}); err != nil || counted {
t.Fatalf("second adoption: counted=%v err=%v", counted, err)
}
// A different PR: counted.
if counted, err = recordContributorAbandon(automerge.ContributorAbandon{Author: "@mallory", Repo: "acme/widget", PR: 8, AdoptedIntoPR: 92, Reason: automerge.AbandonReasonOperatorTakeover, At: at}); err != nil || !counted {
t.Fatalf("second PR: counted=%v err=%v", counted, err)
}

p, err := loadContributorProfile("Mallory")
if err != nil {
t.Fatalf("load: %v", err)
}
if p.Abandoned != 2 || len(p.AbandonedPRs) != 2 {
t.Fatalf("abandoned=%d records=%+v", p.Abandoned, p.AbandonedPRs)
}
want := ContributorAbandonRecord{Repo: "acme/widget", PR: 7, AdoptedIntoPR: 90, Reason: automerge.AbandonReasonOffline, At: "2026-10-30T12:00:00Z"}
if p.AbandonedPRs[0] != want {
t.Fatalf("first record = %+v, want %+v", p.AbandonedPRs[0], want)
}
if r := p.AbandonedPRs[1]; r.PR != 8 || r.AdoptedIntoPR != 92 || r.Reason != automerge.AbandonReasonOperatorTakeover {
t.Fatalf("second record = %+v", r)
}
}

func TestRecordContributorAbandonIgnoresUnknownAuthor(t *testing.T) {
abandonFixture(t)
counted, err := recordContributorAbandon(automerge.ContributorAbandon{Author: "stranger", Repo: "acme/widget", PR: 7, Reason: automerge.AbandonReasonUnresponsive})
if err != nil || counted {
t.Fatalf("unknown author: counted=%v err=%v", counted, err)
}
if _, err := loadContributorProfile("stranger"); err == nil {
t.Fatal("a profile was created for an unregistered author")
}
}

// A writer holding a copy loaded before the adoption must not drop it.
func TestContributorAbandonSurvivesStaleSave(t *testing.T) {
abandonFixture(t)
stale, err := loadContributorProfile("Mallory")
if err != nil {
t.Fatalf("load: %v", err)
}
stale.Version = 0
(&ContributeWSHub{}).Abandoned(context.Background(), automerge.ContributorAbandon{Author: "mallory", Repo: "acme/widget", PR: 7, AdoptedIntoPR: 90, Reason: automerge.AbandonReasonOffline})
stale.TasksCompleted++
if err := saveContributorProfile(stale); err != nil {
t.Fatalf("stale save: %v", err)
}
p, _ := loadContributorProfile("Mallory")
if p.Abandoned != 1 || len(p.AbandonedPRs) != 1 || p.TasksCompleted != 1 {
t.Fatalf("after stale save: abandoned=%d records=%+v tasks=%d", p.Abandoned, p.AbandonedPRs, p.TasksCompleted)
}
}

func TestContributorsListExposesAbandoned(t *testing.T) {
srv := newFullServer(t)
abandonFixture(t)
if _, err := recordContributorAbandon(automerge.ContributorAbandon{Author: "Mallory", Repo: "acme/widget", PR: 7, AdoptedIntoPR: 90, Reason: automerge.AbandonReasonOperatorTakeover}); err != nil {
t.Fatalf("record: %v", err)
}
w := httptest.NewRecorder()
srv.handleContributorsList(w, httptest.NewRequest("GET", "/api/contributors", nil))
var body struct {
Contributors []struct {
GitHubUsername string `json:"github_username"`
Abandoned *int `json:"abandoned"`
AbandonedPRs []ContributorAbandonRecord `json:"abandoned_prs"`
} `json:"contributors"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode: %v\n%s", err, w.Body.String())
}
if len(body.Contributors) != 1 {
t.Fatalf("contributors = %s", w.Body.String())
}
c := body.Contributors[0]
if c.Abandoned == nil || *c.Abandoned != 1 || len(c.AbandonedPRs) != 1 || c.AbandonedPRs[0].AdoptedIntoPR != 90 || c.AbandonedPRs[0].Reason != "operator_takeover" {
t.Fatalf("contributor = %s", w.Body.String())
}
}
10 changes: 10 additions & 0 deletions src/pkg/dashboard/contribute_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ type ContributorProfile struct {
LastActive string `json:"last_active,omitempty"`
LastCompletedTask *WSTaskAssign `json:"last_completed_task,omitempty"`
RateLimits ContributorRateLimits `json:"rate_limits"`
// Abandoned counts PRs of this contributor the hive adopted (in place or
// by supersession, operator takeovers included), one per PR; AbandonedPRs
// holds the records. Written by recordContributorAbandon and append-only
// across saves (hivecommons/hive#15782).
Abandoned int `json:"abandoned"`
AbandonedPRs []ContributorAbandonRecord `json:"abandoned_prs,omitempty"`
// LabelInterests is the contributor's OPT-IN list of GitHub issue labels they
// want to help with (issue #2637) — e.g. a contributor with an NVIDIA machine
// subscribes to "nvidia" so nvidia-labelled work surfaces first for them. It is
Expand Down Expand Up @@ -358,7 +364,11 @@ func saveContributorProfileTokenCAS(p *ContributorProfile, adminOverride, tokenU
p.TokenLastUsedAt = cur.TokenLastUsedAt
}
p.Version = cur.Version
// Abandon records are append-only: a writer holding an older copy
// must not drop one recorded since it loaded.
p.AbandonedPRs = mergeContributorAbandons(cur.AbandonedPRs, p.AbandonedPRs)
}
p.Abandoned = len(p.AbandonedPRs)
p.Version++

ensureDir(getContributorsDir())
Expand Down
68 changes: 68 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_abandon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package automerge

import (
"context"
"strings"
"time"
)

// Abandon reasons carried on a ContributorAbandon (hivecommons/hive#15782):
// the contributor-profile vocabulary, not the stage record's AdoptReason*.
const (
// AbandonReasonOffline: the contributor's relay was offline past the
// offline window after the nudge.
AbandonReasonOffline = "offline"
// AbandonReasonUnresponsive: the grace elapsed with the nudge
// unanswered, or no hive of the author's is registered here.
AbandonReasonUnresponsive = "unresponsive"
// AbandonReasonOperatorTakeover: an operator's Adopt now or an
// owner-delegate's adoption/supersede request.
AbandonReasonOperatorTakeover = "operator_takeover"
)

// ContributorAbandon is one adoption of a contributor's PR by the hive: in
// place (AdoptedIntoPR 0) or by supersession onto hive/adopt-<n>.
type ContributorAbandon struct {
Author string
Repo string // owner/repo
PR int
AdoptedIntoPR int
Reason string
At time.Time
}

// ContributorAbandonSink is the optional third face of the nudge sink: it
// records an adoption against the author's contributor profile so operators
// can see which clankers keep abandoning PRs. Implementations dedupe per PR;
// the engine reports every adoption, including second takeovers.
type ContributorAbandonSink interface {
Abandoned(ctx context.Context, a ContributorAbandon)
}

// abandonReason maps a stage-record adoption reason onto the profile's.
func abandonReason(adoptReason string) string {
switch adoptReason {
case AdoptReasonOperator, AdoptReasonOperatorForced:
return AbandonReasonOperatorTakeover
case AdoptReasonOffline:
return AbandonReasonOffline
}
return AbandonReasonUnresponsive
}

// recordContributorAbandon tells the sink, when it records abandons, that
// the hive adopted obs (into PR into, 0 for in place).
func (c *Engine) recordContributorAbandon(ctx context.Context, obs stageObservation, adoptReason string, into int, now time.Time) {
sink, ok := c.contributorNudge.(ContributorAbandonSink)
if !ok || sink == nil || strings.TrimSpace(obs.Author) == "" || obs.Number <= 0 {
return
}
sink.Abandoned(ctx, ContributorAbandon{
Author: obs.Author,
Repo: obs.Owner + "/" + obs.Repo,
PR: obs.Number,
AdoptedIntoPR: into,
Reason: abandonReason(adoptReason),
At: now.UTC(),
})
}
Loading
Loading