diff --git a/changelog.d/fixed-14708-rework-card-closed-prs.md b/changelog.d/fixed-14708-rework-card-closed-prs.md new file mode 100644 index 0000000000..db84f0b2b6 --- /dev/null +++ b/changelog.d/fixed-14708-rework-card-closed-prs.md @@ -0,0 +1 @@ +- The contributor card's "PRs awaiting rework" count no longer includes PRs that have merged or closed while their stage record still said 072 or 074; a PR is only dropped when its repo's open-PR listing is complete (hivecommons/hive#14708). diff --git a/src/docs/contributor-relay.md b/src/docs/contributor-relay.md index 46488098a0..2a015aeae2 100644 --- a/src/docs/contributor-relay.md +++ b/src/docs/contributor-relay.md @@ -1485,7 +1485,7 @@ Each row of the "Connected contributor agents (ClankeR)" list on `/contribute/op | `fix_queue_depth` | Red PRs waiting in the contributor's server-side fix queue (not dispatched). | | `pending_rework`, `pending_rework_prs[]` | The contributor's open PRs in `stage/072-returned-to-contributor` that have **not** been dispatched to them. The row prints a red **N PRs awaiting rework (not yet dispatched)** line, and an idle relay reads **idle · N PRs awaiting rework** instead of `idle: no matching work`. | -`kind: rework` means the item repairs one of the contributor's own PRs: any `fix-pr` task, or a task whose target the stage store has in a repair stage (`070-fixing`, `071-needs-author`, `072-returned-to-contributor`, `074-adopted-by-hive`). Everything else is `new`. `pending_rework` is derived without a GitHub call: the stage-label store (which remembers each PR's author) ∪ the fix queue, minus the fix tasks in flight on any of the contributor's connections. A non-zero `pending_rework` on an idle row is the signal to look at — typically a relay that did not advertise `fix_pr_interrupt_v1`, a preempt-mode relay inside its `contribute.min_focus_window`, or a contributor who is offline. +`kind: rework` means the item repairs one of the contributor's own PRs: any `fix-pr` task, or a task whose target the stage store has in a repair stage (`070-fixing`, `071-needs-author`, `072-returned-to-contributor`, `074-adopted-by-hive`). Everything else is `new`. `pending_rework` is derived without a GitHub call: the stage-label store (which remembers each PR's author) ∪ the fix queue, minus the fix tasks in flight on any of the contributor's connections, minus any PR the status snapshot shows as merged or closed. A stage record can outlive its PR when the stage sweep never saw it close, so a PR is dropped when its repo's open-PR list is complete (enumerated, not paused, not incomplete, stale or carrying unlisted PRs) and does not include it; with no complete listing the record is kept. A non-zero `pending_rework` on an idle row is the signal to look at — typically a relay that did not advertise `fix_pr_interrupt_v1`, a preempt-mode relay inside its `contribute.min_focus_window`, or a contributor who is offline. ## Reconnecting without losing in-flight work diff --git a/src/pkg/dashboard/contribute_fleet.go b/src/pkg/dashboard/contribute_fleet.go index b80cdb5459..b4c105b066 100644 --- a/src/pkg/dashboard/contribute_fleet.go +++ b/src/pkg/dashboard/contribute_fleet.go @@ -227,6 +227,7 @@ type FleetSnapshot struct { // last pong is older than wsHeartbeatTimeout is reported with Stale=true and its // in-flight task is treated as no longer active (matching LiveStates()). func (h *ContributeWSHub) FleetSnapshot() FleetSnapshot { + openPRs := h.openPRIndex() h.mu.RLock() defer h.mu.RUnlock() snap := FleetSnapshot{ @@ -326,7 +327,7 @@ func (h *ContributeWSHub) FleetSnapshot() FleetSnapshot { if task == nil { fc.IdleReason = c.lastIdleReason } - fc.FleetRework = h.fleetReworkFor(c, fc.Stale) + fc.FleetRework = h.fleetReworkFor(c, fc.Stale, openPRs) c.mu.Unlock() fc.CurrentTask = task fc.PromptPreview = promptPreview diff --git a/src/pkg/dashboard/contribute_fleet_rework.go b/src/pkg/dashboard/contribute_fleet_rework.go index cc44a499a2..aed0851250 100644 --- a/src/pkg/dashboard/contribute_fleet_rework.go +++ b/src/pkg/dashboard/contribute_fleet_rework.go @@ -138,9 +138,62 @@ func workKindFor(store *stagelabels.Store, taskKind, repo string, number int) (k return kind, stage } +// openPRIndex is the status snapshot's canonical open-PR set (repoOpenItems: +// actionable, held, drafts and sidelined rows), keyed like the stage store. +// It answers "is this PR closed?" only for a repo whose PR list is complete; +// for any other repo, or with no snapshot yet, a PR counts as open. +type openPRIndex struct { + repos map[string]bool // lower-cased owner/repo with a complete PR list + prs map[string]bool // stagelabels.Key of every listed open PR +} + +// openPRIndex builds the index from the hub server's current status. A repo +// whose rows cannot vouch for a departure (ownershipRowsComplete: counts +// incomplete, paused, or never enumerated), or whose counts are stale or carry +// unlisted PRs, is left out: a PR missing from such a listing is not evidence +// that it closed. +func (h *ContributeWSHub) openPRIndex() openPRIndex { + var idx openPRIndex + if h == nil || h.server == nil { + return idx + } + h.server.statusMu.RLock() + status := h.server.status + h.server.statusMu.RUnlock() + if status == nil { + return idx + } + idx.repos, idx.prs = map[string]bool{}, map[string]bool{} + for _, repo := range status.Repos { + if !ownershipRowsComplete(repo) || repo.CountsStale || repo.UnlistedPRs > 0 { + continue + } + full := nonEmpty(repo.Full, repo.Name) + idx.repos[strings.ToLower(full)] = true + for _, it := range repoOpenItems(repo) { + if it.Kind == "pr" { + idx.prs[stagelabels.Key(full, it.Number)] = true + } + } + } + return idx +} + +// closed reports whether the snapshot shows repo#number as no longer open: +// the repo's PR list is complete and does not carry it +// (hivecommons/hive#14708). +func (idx openPRIndex) closed(repo string, number int) bool { + if !idx.repos[strings.ToLower(strings.TrimSpace(repo))] { + return false + } + return !idx.prs[stagelabels.Key(repo, number)] +} + // fleetReworkFor builds the rework view for one connection. Call with c.mu -// held; it takes only the fix scheduler's and stage store's own locks. -func (h *ContributeWSHub) fleetReworkFor(c *ContributorConnection, stale bool) FleetRework { +// held; it takes only the fix scheduler's and stage store's own locks. open +// drops PRs that have merged or closed: stage records outlive a PR whose +// close the stage sweep never observed (hivecommons/hive#14708). +func (h *ContributeWSHub) fleetReworkFor(c *ContributorConnection, stale bool, open openPRIndex) FleetRework { var out FleetRework store := h.stageStoreOrShared() cap := h.fixCapacity(c) @@ -212,7 +265,7 @@ func (h *ContributeWSHub) fleetReworkFor(c *ContributorConnection, stale bool) F } pending := map[string]FleetReworkPR{} for _, it := range queued { - if inFlightKeys[it.Key()] { + if inFlightKeys[it.Key()] || open.closed(it.Repo, it.Number) { continue } cause := it.Cause.Check @@ -233,7 +286,7 @@ func (h *ContributeWSHub) fleetReworkFor(c *ContributorConnection, stale bool) F continue } repo, number := splitStageKey(ai.Key) - if repo == "" || number == 0 { + if repo == "" || number == 0 || open.closed(repo, number) { continue } pr := FleetReworkPR{Repo: repo, Number: number, Stage: ai.Record.Stage.Label()} @@ -250,7 +303,7 @@ func (h *ContributeWSHub) fleetReworkFor(c *ContributorConnection, stale bool) F continue } repo, number := splitStageKey(ai.Key) - if repo == "" || number == 0 { + if repo == "" || number == 0 || open.closed(repo, number) { continue } pr := FleetReworkPR{Repo: repo, Number: number, Stage: ai.Record.Stage.Label(), State: FleetReworkStateAdopted} diff --git a/src/pkg/dashboard/contribute_fleet_rework_14708_test.go b/src/pkg/dashboard/contribute_fleet_rework_14708_test.go new file mode 100644 index 0000000000..e60107d7f3 --- /dev/null +++ b/src/pkg/dashboard/contribute_fleet_rework_14708_test.go @@ -0,0 +1,118 @@ +package dashboard + +import ( + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// hivecommons/hive#14708: a contributor card said "4 PRs awaiting rework" +// when three of them had merged two days earlier still wearing 072. The +// stage records outlived the PRs, so the card must check the PR is open. + +func setReworkStatus(srv *Server, repos ...FrontendRepo) { + srv.statusMu.Lock() + srv.status = &StatusPayload{Repos: repos} + srv.statusMu.Unlock() +} + +func noteRework(store *stagelabels.Store, repo string, n int, stage stagelabels.Stage, author string) { + store.Observe(stagelabels.Key(repo, n), stage, "check-failing:build") + store.NoteAuthor(stagelabels.Key(repo, n), author) +} + +func pendingNumbers(fc FleetClanker) []int { + var out []int + for _, pr := range fc.PendingReworkPRs { + out = append(out, pr.Number) + } + return out +} + +func TestFleetReworkExcludesMergedAndClosedPRs(t *testing.T) { + hub, srv, store := reworkHub(t) + fixConn(hub, "alice", fixCaps(capFixPRInterrupt), time.Now()) + + noteRework(store, "org/r", 1, stagelabels.ReturnedToContributor, "alice") // open + noteRework(store, "org/r", 2, stagelabels.ReturnedToContributor, "alice") // merged + noteRework(store, "org/r", 3, stagelabels.AdoptedByHive, "alice") // closed + setReworkStatus(srv, FrontendRepo{Name: "r", Full: "org/r", OpenPrs: []any{ + FrontendPR{PullRequest: github.PullRequest{Repo: "org/r", Number: 1, Author: "alice"}}, + }}) + + fc := snapshotFor(t, hub, "alice") + if got := pendingNumbers(fc); fc.PendingRework != 1 || len(got) != 1 || got[0] != 1 { + t.Fatalf("pending_rework=%d prs=%v, want only the open #1", fc.PendingRework, got) + } +} + +func TestFleetReworkKeepsOpenDraftAndAdoptedPRs(t *testing.T) { + hub, srv, store := reworkHub(t) + fixConn(hub, "alice", fixCaps(capFixPRInterrupt), time.Now()) + + noteRework(store, "org/r", 4, stagelabels.ReturnedToContributor, "alice") + noteRework(store, "org/r", 5, stagelabels.AdoptedByHive, "alice") + // A draft is not in OpenPrs; the enumeration carries it as a sidelined + // row, which is still the canonical open set. + setReworkStatus(srv, FrontendRepo{Name: "r", Full: "org/r", + OpenPrs: []any{FrontendPR{PullRequest: github.PullRequest{Repo: "org/r", Number: 5}}}, + NonActionablePrs: []any{github.RepoWorkPR{Repo: "org/r", Number: 4, Draft: true, Bucket: "draft"}}, + }) + + fc := snapshotFor(t, hub, "alice") + if got := pendingNumbers(fc); fc.PendingRework != 2 || len(got) != 2 || got[0] != 4 || got[1] != 5 { + t.Fatalf("pending_rework=%d prs=%v, want open #4 (draft) and #5 (074)", fc.PendingRework, got) + } +} + +func TestFleetReworkIncompleteListingKeepsRecords(t *testing.T) { + cases := map[string]FrontendRepo{ + "incomplete": {Name: "r", Full: "org/r", CountsIncomplete: true}, + "stale": {Name: "r", Full: "org/r", CountsStale: true}, + "unlisted": {Name: "r", Full: "org/r", UnlistedPRs: 1}, + "paused": {Name: "r", Full: "org/r", Paused: true}, + // Pre-scan shape: built before the first eval cycle (no actionable + // snapshot), so no counts or item lists are set at all. + "never enumerated": {Name: "r", Full: "org/r", ownershipDataMissing: true}, + "other repo": {Name: "q", Full: "org/q"}, + } + for name, repo := range cases { + t.Run(name, func(t *testing.T) { + hub, srv, store := reworkHub(t) + fixConn(hub, "alice", fixCaps(capFixPRInterrupt), time.Now()) + noteRework(store, "org/r", 1, stagelabels.ReturnedToContributor, "alice") + setReworkStatus(srv, repo) + + fc := snapshotFor(t, hub, "alice") + if got := pendingNumbers(fc); fc.PendingRework != 1 || len(got) != 1 || got[0] != 1 { + t.Fatalf("pending_rework=%d prs=%v: a PR missing from a listing that cannot vouch for it must stay", fc.PendingRework, got) + } + }) + } +} + +func TestFleetReworkNoStatusKeepsRecords(t *testing.T) { + hub, _, store := reworkHub(t) + fixConn(hub, "alice", fixCaps(capFixPRInterrupt), time.Now()) + noteRework(store, "org/r", 1, stagelabels.ReturnedToContributor, "alice") + + fc := snapshotFor(t, hub, "alice") + if fc.PendingRework != 1 { + t.Fatalf("pending_rework=%d, want 1 before the first status snapshot", fc.PendingRework) + } +} + +func TestOpenPRIndexClosedIsCaseInsensitive(t *testing.T) { + idx := openPRIndex{repos: map[string]bool{"org/r": true}, prs: map[string]bool{stagelabels.Key("org/r", 1): true}} + if idx.closed("Org/R", 1) { + t.Fatal("#1 is listed open") + } + if !idx.closed("Org/R", 2) { + t.Fatal("#2 is absent from a complete listing") + } + if (openPRIndex{}).closed("org/r", 2) { + t.Fatal("an empty index knows nothing") + } +}