diff --git a/changelog.d/fixed-14606.md b/changelog.d/fixed-14606.md new file mode 100644 index 0000000000..86494349f0 --- /dev/null +++ b/changelog.d/fixed-14606.md @@ -0,0 +1 @@ +- The stage tracker now fetches a title and state for same-repo issues a PR links only softly (`refs`, title or mention references), so they no longer render as "(title unavailable)". Lookups are memoised per issue across tracker rebuilds and bounded per rebuild (at most 10 GETs within 3 s), so a status publish no longer re-queries GitHub while the tracker lock is held (#14606). diff --git a/changelog.d/fixed-14667.md b/changelog.d/fixed-14667.md new file mode 100644 index 0000000000..a930f86a30 --- /dev/null +++ b/changelog.d/fixed-14667.md @@ -0,0 +1 @@ +- The stage tracker no longer binds a PR to a sibling issue its first paragraph says it leaves for later (`#N … remain`, or a `#N` inside the parenthetical qualifying another reference such as `Part of #14550 (tracker; #14559 … remain)`), and drops links whose number is a pull request, open or closed (`Supersedes #N`), instead of drawing them as an issue "(title unavailable)" (#14667). diff --git a/src/docs/api-reference.md b/src/docs/api-reference.md index 0677f9aa8c..08c74f9c68 100644 --- a/src/docs/api-reference.md +++ b/src/docs/api-reference.md @@ -13,17 +13,17 @@ History point limits, timestamp cutoffs, incremental cost reads and ETag/304 beh | `GET` | `/api/version` | Dashboard auth/session | Build/version metadata; includes `upgradeMarker` (`target`, `current`, `attempts`, `maxAttempts`, `failed`, `requestedAt`, `lastError`) while a self-upgrade is in flight or has failed ([#6765](https://github.com/hivecommons/hive/issues/6765)), the hub-delivered `upgradePolicy` (`hub_managed`, `spoke_managed`, `schedule`, `schedule_hour`, `schedule_timezone`, `schedule_weekday`, `paused`, `branch`, `channel`, `target_sha`, `target_resolved`, `armed_target`, and `next_update_at` — the hub's RFC3339 ETA for the next promotion into the `stable` channel, omitted when unknown ([#10256](https://github.com/hivecommons/hive/issues/10256))) when available, and an `autoUpdate` object (`enabled`, `state` — one of `disabled`/`up_to_date`/`behind`/`retrying`/`failed`/`unknown` — `healthy`, `period`, `targetBranch`, `targetCommit`, `currentCommit`, `commitsBehind`, `lastAttemptAt`, `lastError`, `detail`, and `nextUpdateAt` — the hub's RFC3339 ETA for the next update, omitted when unknown, disabled or paused ([#10257](https://github.com/hivecommons/hive/issues/10257))) that never reports a failed or unknown update as healthy ([#6962](https://github.com/hivecommons/hive/issues/6962), [#6963](https://github.com/hivecommons/hive/issues/6963)) | `pkg/dashboard/api.go:42` | | `GET` | `/api/version/release-notes` | Dashboard auth/session | Changelog gap between two revisions: `?from=&to=` returns `{from:{sha,version?}, to:{sha,ref,version?}, sections:[{version,date,categories:{added,changed,fixed,security,deprecated}}], unreleased?, truncated, source, error?}` — the `CHANGELOG.md` release sections present at `to` and absent at `from`, newest first, plus (when `to` is not a release tag) the `changelog.d/` fragments new at `to` under an `Unreleased (in this build)` section. Fetch failures return `source: "unavailable"` with `error` rather than an error status; results are cached per (from, to) for an hour ([#11065](https://github.com/hivecommons/hive/issues/11065)) | `pkg/dashboard/api.go:43` | | `POST` | `/api/release-channel` | Owner only | Hosted spoke self-service release-channel selector; relays `stable`/`candidate`/`edge` to the hub's existing switch-branch endpoint with the spoke dashboard-token proof, and reports the requested channel as pending until the Deployment image lands | `pkg/dashboard/api.go:64` | -| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1502` | -| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1503` | -| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1504` | -| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1510` | -| `GET` | `/api/status` | Dashboard auth/session | Dashboard aggregate status; `labelPresets[]` (`{key,label,description,labels[],states[]}`) lists the built-in label-filter presets for the Projects cards, led by `needs-human` whose `labels[]` are the effective hard-suppress labels plus `needs-signal`, the hold labels, `stage/071-needs-author` and the reporter-trust awaiting label (#12172); accepts `?fields=a,b` for top-level selection and `?omit=repos` to drop heavy sections, and honors `Accept-Encoding: gzip`; `agents[]` includes busy-pane visibility fields (`kicksUndeliverable`, `busySince`, `lastTranscriptActivity`, `condition`, `conditionMessage`) when a Working pane cannot accept kicks; a working agent also carries `agents[].run` — the live per-run line (#14576): `{since, elapsedS, costUSD, tokens, priced, tests, filesChanged}`, the estimated spend/tokens since the kick was delivered (`lastKickAt`, falling back to `busySince`) (delta of the token collector's per-agent cumulative figure; `priced:false` when the agent has none yet), the last visible test-runner result (`pass`/`fail`/empty) and the last `N files changed` git summary (`-1` when none) parsed from the captured pane; top-level `fleetRun` `{running, hourUSD, hourPriced}` is the governor strip's `running N · $X this hour` figure; `sla` (fleet) and `repos[].sla` (per repo) carry the Delivery SLA block derived from the lifecycle timeline with no extra GitHub calls: `windows.{7d,30d}.{open_to_triage,triage_to_start,start_to_pr,pr_to_merge,open_to_merge}` as `{p50_h,p95_h,n}` (hours, nearest-rank), `history[]` of up to 30 daily buckets `{day,start_to_pr_p50,pr_to_merge_p50,open_to_merge_p50,n_started,n_pr,n_merged}` persisted under `/data/sla-history.json`, `stale_in_progress[]` (`{number,repo,started_at,age_h,agent}` — issues carrying `hive/in-progress` longer than the agent claim TTL with no linked PR), `stale_count`, `merged_7d`, `claim_ttl_s` and, on the fleet block, the effective `targets` (`start_to_pr_h`, `pr_to_merge_h`, `open_to_merge_h`); `staleSince` and `repos[].countsStale` / `countsStaleSince` mark totals served from last-good figures — the previous enumeration (#13382) or the ForgeState cache's last-good counts (#13376) | `pkg/dashboard/server.go:1515` | -| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1516` | -| `GET` | `/api/overview/issues.csv` | Dashboard auth/session | Cached Overview issue-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1517` | -| `GET` | `/api/overview/issues.json` | Dashboard auth/session | Cached Overview issue-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1518` | -| `GET` | `/api/overview/prs.csv` | Dashboard auth/session | Cached Overview PR-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1519` | -| `GET` | `/api/overview/prs.json` | Dashboard auth/session | Cached Overview PR-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1520` | -| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1521` | +| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1505` | +| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1506` | +| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1507` | +| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1513` | +| `GET` | `/api/status` | Dashboard auth/session | Dashboard aggregate status; `labelPresets[]` (`{key,label,description,labels[],states[]}`) lists the built-in label-filter presets for the Projects cards, led by `needs-human` whose `labels[]` are the effective hard-suppress labels plus `needs-signal`, the hold labels, `stage/071-needs-author` and the reporter-trust awaiting label (#12172); accepts `?fields=a,b` for top-level selection and `?omit=repos` to drop heavy sections, and honors `Accept-Encoding: gzip`; `agents[]` includes busy-pane visibility fields (`kicksUndeliverable`, `busySince`, `lastTranscriptActivity`, `condition`, `conditionMessage`) when a Working pane cannot accept kicks; a working agent also carries `agents[].run` — the live per-run line (#14576): `{since, elapsedS, costUSD, tokens, priced, tests, filesChanged}`, the estimated spend/tokens since the kick was delivered (`lastKickAt`, falling back to `busySince`) (delta of the token collector's per-agent cumulative figure; `priced:false` when the agent has none yet), the last visible test-runner result (`pass`/`fail`/empty) and the last `N files changed` git summary (`-1` when none) parsed from the captured pane; top-level `fleetRun` `{running, hourUSD, hourPriced}` is the governor strip's `running N · $X this hour` figure; `sla` (fleet) and `repos[].sla` (per repo) carry the Delivery SLA block derived from the lifecycle timeline with no extra GitHub calls: `windows.{7d,30d}.{open_to_triage,triage_to_start,start_to_pr,pr_to_merge,open_to_merge}` as `{p50_h,p95_h,n}` (hours, nearest-rank), `history[]` of up to 30 daily buckets `{day,start_to_pr_p50,pr_to_merge_p50,open_to_merge_p50,n_started,n_pr,n_merged}` persisted under `/data/sla-history.json`, `stale_in_progress[]` (`{number,repo,started_at,age_h,agent}` — issues carrying `hive/in-progress` longer than the agent claim TTL with no linked PR), `stale_count`, `merged_7d`, `claim_ttl_s` and, on the fleet block, the effective `targets` (`start_to_pr_h`, `pr_to_merge_h`, `open_to_merge_h`); `staleSince` and `repos[].countsStale` / `countsStaleSince` mark totals served from last-good figures — the previous enumeration (#13382) or the ForgeState cache's last-good counts (#13376) | `pkg/dashboard/server.go:1518` | +| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1519` | +| `GET` | `/api/overview/issues.csv` | Dashboard auth/session | Cached Overview issue-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1520` | +| `GET` | `/api/overview/issues.json` | Dashboard auth/session | Cached Overview issue-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1521` | +| `GET` | `/api/overview/prs.csv` | Dashboard auth/session | Cached Overview PR-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1522` | +| `GET` | `/api/overview/prs.json` | Dashboard auth/session | Cached Overview PR-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1523` | +| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1524` | | `GET` | `/api/swarm` | Dashboard auth/session | Current swarm status: display name, duration, active repo, expiry, persisted prep metrics when available, and idle-unlock fields. | `pkg/dashboard/swarm.go:310` | | `POST` | `/api/swarm` | Owner only | Start a 24h repo swarm for a configured repo, returning 409 when another swarm is active and 423 when a previous swarm keeps the next swarm locked until enough agents are idle unless body `{"repo":"owner/name","force":true}` is used; saves the active record before repo prep, persists prep metrics, and announces the start to Discord when configured. | `pkg/dashboard/swarm.go:311` | | `DELETE` | `/api/swarm` | Owner only | End the active swarm, score issues closed and PRs merged in the window, append it to history, and announce the result to Discord when configured. | `pkg/dashboard/swarm.go:312` | @@ -66,7 +66,7 @@ History point limits, timestamp cutoffs, incremental cost reads and ETag/304 beh | Method | Path | Auth | Purpose | Source | |---|---|---|---|---| | `GET` | `/api/style` | Public | Sanitized custom dashboard CSS | `pkg/dashboard/api.go:44` | -| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](branding.md)) | `pkg/dashboard/server.go:1582` | +| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](branding.md)) | `pkg/dashboard/server.go:1585` | | `GET` | `/api/snapshot/frame-ancestors` | Public | Snapshot framing allowlist | `pkg/dashboard/api.go:77` | | `GET` | `/api/snapshot` | Public | Snapshot data | `pkg/dashboard/api.go:78` | | `GET` | `/snapshot` | Public | Public read-only snapshot page | `pkg/dashboard/api.go:79` | @@ -94,9 +94,9 @@ History point limits, timestamp cutoffs, incremental cost reads and ETag/304 beh | `GET` | `/api/openrouter/models` | Dashboard auth/session | Open Router Models | `pkg/dashboard/openrouter.go:47` | | `GET` | `/api/openrouter/credit` | Dashboard auth/session | Open Router Credit | `pkg/dashboard/openrouter.go:48` | | `GET` | `/openrouter/callback` | Public | Open Router Callback | `pkg/dashboard/openrouter.go:49` | -| `POST` | `/api/github-app/recheck` | Dashboard auth/session | GitHub App Recheck | `pkg/dashboard/server.go:1522` | -| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1523` | -| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1524` | +| `POST` | `/api/github-app/recheck` | Dashboard auth/session | GitHub App Recheck | `pkg/dashboard/server.go:1525` | +| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1526` | +| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1527` | ## Configuration @@ -593,7 +593,7 @@ always resolved server-side from the validated token. | `POST` | `/api/hives/{id}/heartbeat` | Dashboard auth/session | Hives Heartbeat | `pkg/dashboard/api_contribute.go:275` | | `DELETE` | `/api/hives/{id}` | Owner only | Hives Delete | `pkg/dashboard/api_contribute.go:276` | | `POST` | `/api/hives/onboard` | Dashboard auth/session | Hives Onboard | `pkg/dashboard/api_contribute.go:277` | -| `GET` | `/sso` | Public | SSO | `pkg/dashboard/server.go:1529` | +| `GET` | `/sso` | Public | SSO | `pkg/dashboard/server.go:1532` | ## Hub SaaS diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index faad2bc3a3..d25d2b77e6 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -131,6 +131,9 @@ type Server struct { // stageTrackerBuildFn replaces buildStageTracker in tests that need a // build they can hold open; nil in production. stageTrackerBuildFn func(status *StatusPayload, store *stagelabels.Store, now time.Time) StageTrackerResponse + // stageTrackerLinks memoises the issue lookups that title the tracker's + // untitled links across rebuilds (stage_tracker.go). + stageTrackerLinks stageTrackerLinkMemo // evidenceHB is the background-refreshed evidence aggregate the hub // heartbeat reads (evidence_heartbeat.go); zero value is ready to use. evidenceHB evidenceHeartbeatCache diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 1a16c99db8..281077acf6 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -11,6 +11,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" "github.com/hivecommons/hive/pkg/claims" @@ -1546,8 +1547,9 @@ func repoWorkPR(raw any) (github.RepoWorkPR, bool) { } // stageTrackerRESTCaller tags the endpoint's requests in the REST -// accounting (/api/gh-rate-limits) so an operator can see them listed with -// Charged 0: the handler never reaches GitHub. +// accounting (/api/gh-rate-limits). Serving the tracker is Charged 0; the +// only GitHub reads are the memoised issues GETs that title untitled links +// (stageTrackerEnrichSoftLinks). const stageTrackerRESTCaller = "dashboard:stage-tracker" // stageTrackerCache is the memoised payload: the encoded body, its ETag and @@ -1670,6 +1672,7 @@ func (s *Server) encodeStageTracker(status *StatusPayload, now time.Time, ttl ti build = s.stageTrackerBuildFn } resp := build(status, s.stageStoreOrShared(), now) + s.stageTrackerEnrichSoftLinks(resp.Items, stageTrackerRepoQualifier(status), now) resp.CacheTTLSeconds = int64(ttl / time.Second) // The encoded body carries source=cache: that is what every later hit // serves verbatim, and what the ETag validates. The building request @@ -1848,11 +1851,128 @@ func stageTrackerLinkSoft(li StageTrackerLinkedIssue) bool { return li.Via != "" && li.Via != github.PRLinkViaFixes } +// Bounds on the issues GETs stageTrackerEnrichSoftLinks makes. A lookup is +// memoised per Server for stageTrackerLinkTTL (a failed one for +// stageTrackerLinkRetry), because the tracker payload itself is rebuilt on +// every status publish. A rebuild makes at most stageTrackerLinkLookups GETs, +// all within stageTrackerLinkTimeout, as it holds stageTrackerCacheMu. +const ( + stageTrackerLinkTTL = 15 * time.Minute + stageTrackerLinkRetry = 2 * time.Minute + stageTrackerLinkLookups = 10 + stageTrackerLinkTimeout = 3 * time.Second + stageTrackerLinkMemoMax = 1024 +) + +// stageTrackerLinkInfo is one memoised lookup: the issue's title and state, +// or pr when the number names a pull request; ok is false for a failed GET. +type stageTrackerLinkInfo struct { + title, state string + pr, ok bool + at time.Time +} + +// stageTrackerLinkMemo memoises link lookups keyed by owner/repo#number. +type stageTrackerLinkMemo struct { + mu sync.Mutex + m map[string]stageTrackerLinkInfo +} + +func (m *stageTrackerLinkMemo) get(key string, now time.Time) (stageTrackerLinkInfo, bool) { + m.mu.Lock() + defer m.mu.Unlock() + in, ok := m.m[key] + if ok && now.Sub(in.at) < in.ttl() { + return in, true + } + return stageTrackerLinkInfo{}, false +} + +func (m *stageTrackerLinkMemo) put(key string, in stageTrackerLinkInfo, now time.Time) { + m.mu.Lock() + defer m.mu.Unlock() + if m.m == nil { + m.m = map[string]stageTrackerLinkInfo{} + } + if len(m.m) >= stageTrackerLinkMemoMax { + for k, v := range m.m { + if now.Sub(v.at) >= v.ttl() { + delete(m.m, k) + } + } + } + in.at = now + m.m[key] = in +} + +func (in stageTrackerLinkInfo) ttl() time.Duration { + if in.ok { + return stageTrackerLinkTTL + } + return stageTrackerLinkRetry +} + +// stageTrackerEnrichSoftLinks gives the links no listed issue or closing +// reference titled (soft refs / title / mention links, and closing keywords +// hive read from the PR text) a title and state, from one issues GET per +// distinct same-repo number, memoised across rebuilds (stageTrackerLinkMemo). +// A number that turns out to be a pull request, open or closed (`Supersedes +// #N`, an adoption's "close #N"), is dropped: it is not an issue the PR +// covers (hivecommons/hive#14667). A failed or skipped lookup leaves the link +// as it was. +func (s *Server) stageTrackerEnrichSoftLinks(items []StageTrackerItem, qualify func(string) string, now time.Time) { + if s == nil || s.deps == nil || s.deps.GHClient == nil { + return + } + ctx, cancel := context.WithTimeout(github.WithRESTCaller(context.Background(), stageTrackerRESTCaller), stageTrackerLinkTimeout) + defer cancel() + lookups := 0 + for i := range items { + pr := &items[i] + if pr.Kind != "pr" || len(pr.LinkedIssues) == 0 { + continue + } + owner, name, ok := strings.Cut(qualify(pr.Repo), "/") + if !ok { + continue + } + kept := pr.LinkedIssues[:0] + for _, li := range pr.LinkedIssues { + if !li.Listed && li.Title == "" && li.State == "" { + key := owner + "/" + name + "#" + strconv.Itoa(li.Number) + in, hit := s.stageTrackerLinks.get(key, now) + if !hit && lookups < stageTrackerLinkLookups && ctx.Err() == nil { + lookups++ + title, state, isPR, err := s.deps.GHClient.IssueTitleState(ctx, owner, name, li.Number) + in = stageTrackerLinkInfo{title: title, state: state, pr: isPR, ok: err == nil} + // Running out of this rebuild's budget says nothing + // about the issue: leave it for the next rebuild. + if err == nil || ctx.Err() == nil { + s.stageTrackerLinks.put(key, in, now) + } + } + if in.pr { + continue + } + if in.ok { + li.Title, li.State = in.title, in.state + } + } + kept = append(kept, li) + } + if len(kept) == 0 { + kept = nil + } + pr.LinkedIssues = kept + } +} + // stageTrackerPruneSoftLinks drops, from a PR's links, every soft link whose // number is an open PR in the same repo (a squash title's `(#N)` or a // `Refs #N` naming the PR this one follows) and every bare mention that is // not an issue listed in the response — a keyword-less `#N` has no other -// way to prove it names an issue. +// way to prove it names an issue. A closed PR is not in the response; +// stageTrackerEnrichSoftLinks drops links to those. func stageTrackerPruneSoftLinks(pr *StageTrackerItem, prs, issues map[string]*StageTrackerItem) { if len(pr.LinkedIssues) == 0 { return diff --git a/src/pkg/dashboard/stage_tracker_test.go b/src/pkg/dashboard/stage_tracker_test.go index a23a2d8995..110afb7448 100644 --- a/src/pkg/dashboard/stage_tracker_test.go +++ b/src/pkg/dashboard/stage_tracker_test.go @@ -2,12 +2,14 @@ package dashboard import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" "path/filepath" "slices" "strconv" "strings" + "sync/atomic" "testing" "time" @@ -1003,3 +1005,93 @@ func TestStageTrackerClosedIssueAnchor(t *testing.T) { }) } } + +// hivecommons/hive#14606: a soft link (refs / title / mention) carries only a +// number, so the tracker fetches its title and state once per distinct issue +// and memoises it across rebuilds, since every status publish rebuilds the +// payload. hivecommons/hive#14667: a number that is a PR, open or closed +// (`Supersedes #14592`, an adoption's "close #14592"), is dropped rather than +// drawn as an issue "(title unavailable)". +func TestStageTrackerEnrichSoftLinks(t *testing.T) { + var gets atomic.Int32 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gets.Add(1) + switch r.URL.Path { + case "/repos/hivecommons/hive/issues/14576": + fmt.Fprint(w, `{"number":14576,"title":"Runner stalls","state":"closed"}`) + case "/repos/hivecommons/hive/issues/14577": + fmt.Fprint(w, `{"number":14577,"title":"A PR","state":"open","pull_request":{}}`) + case "/repos/hivecommons/hive/issues/14592": + fmt.Fprint(w, `{"number":14592,"title":"Superseded PR","state":"closed","pull_request":{}}`) + default: + http.NotFound(w, r) + } + })) + defer ts.Close() + s := newTestServer() + s.deps = &Dependencies{GHClient: github.NewClientForTest(ts.URL, "hivecommons", []string{"hive"}, s.logger)} + url := func(n int) string { return "https://github.com/hivecommons/hive/issues/" + strconv.Itoa(n) } + build := func() []StageTrackerItem { + return []StageTrackerItem{ + {Repo: "hive", Number: 14597, Kind: "pr", LinkedIssues: []StageTrackerLinkedIssue{ + {Number: 14576, URL: url(14576), Via: github.PRLinkViaRefs}, + {Number: 14577, URL: url(14577), Via: github.PRLinkViaTitle}, + {Number: 14578, URL: url(14578), Via: github.PRLinkViaRefs}, + {Number: 14567, Title: "Titled", State: "closed", URL: url(14567)}, + {Number: 14592, URL: url(14592)}, + }}, + {Repo: "hive", Number: 14590, Kind: "pr", LinkedIssues: []StageTrackerLinkedIssue{{Number: 14576, URL: url(14576), Via: github.PRLinkViaRefs}}}, + } + } + qualify := func(string) string { return "hivecommons/hive" } + now := time.Date(2026, 10, 27, 18, 0, 0, 0, time.UTC) + items := build() + s.stageTrackerEnrichSoftLinks(items, qualify, now) + + links := items[0].LinkedIssues + if len(links) != 3 { + t.Fatalf("links = %+v, want the PR numbers 14577 and 14592 dropped", links) + } + if got := links[0]; got.Number != 14576 || got.Title != "Runner stalls" || got.State != "closed" { + t.Fatalf("refs link = %+v, want titled and closed", got) + } + if got := items[1].LinkedIssues[0]; got.Title != "Runner stalls" || got.State != "closed" { + t.Fatalf("second PR's refs link = %+v", got) + } + if got := links[1]; got.Number != 14578 || got.Title != "" || got.State != "" { + t.Fatalf("failed lookup = %+v, want untouched", got) + } + if got := links[2]; got.Number != 14567 || got.Title != "Titled" || got.State != "closed" { + t.Fatalf("closing link = %+v, want unchanged", got) + } + if n := gets.Load(); n != 4 { + t.Fatalf("issues GETs = %d, want 4 (one per distinct number)", n) + } + + // A rebuild after a status publish reuses the memo, the failed lookup + // included, until its retry window lapses. + items = build() + s.stageTrackerEnrichSoftLinks(items, qualify, now.Add(time.Minute)) + if n := gets.Load(); n != 4 || len(items[0].LinkedIssues) != 3 || items[0].LinkedIssues[0].Title != "Runner stalls" { + t.Fatalf("rebuild: GETs = %d, links = %+v, want memoised", n, items[0].LinkedIssues) + } + s.stageTrackerEnrichSoftLinks(build(), qualify, now.Add(stageTrackerLinkRetry)) + if n := gets.Load(); n != 5 { + t.Fatalf("issues GETs = %d, want only the failed lookup retried", n) + } + s.stageTrackerEnrichSoftLinks(build(), qualify, now.Add(stageTrackerLinkTTL)) + if n := gets.Load(); n != 9 { + t.Fatalf("issues GETs = %d, want every lookup refreshed after the TTL", n) + } + + // One rebuild makes at most stageTrackerLinkLookups GETs. + var many []StageTrackerLinkedIssue + for n := 20000; n < 20000+stageTrackerLinkLookups+5; n++ { + many = append(many, StageTrackerLinkedIssue{Number: n, URL: url(n), Via: github.PRLinkViaRefs}) + } + before := gets.Load() + s.stageTrackerEnrichSoftLinks([]StageTrackerItem{{Repo: "hive", Number: 14598, Kind: "pr", LinkedIssues: many}}, qualify, now) + if n := gets.Load() - before; n != stageTrackerLinkLookups { + t.Fatalf("issues GETs in one rebuild = %d, want the cap %d", n, stageTrackerLinkLookups) + } +} diff --git a/src/pkg/github/advisory_work.go b/src/pkg/github/advisory_work.go index 3ef25306a9..0a7a84bd31 100644 --- a/src/pkg/github/advisory_work.go +++ b/src/pkg/github/advisory_work.go @@ -46,3 +46,21 @@ func (c *Client) AdvisoryWorkState(ctx context.Context, owner, repo string, numb work.CheckedAt = time.Now() return work, nil } + +// IssueTitleState reads an issue's title and lower-cased state with one issues +// GET. A number that names a pull request, open or closed, yields empty +// strings and isPR: the caller only wants an issue's title, and a PR must not +// be drawn in an issue's place. +func (c *Client) IssueTitleState(ctx context.Context, owner, repo string, number int) (title, state string, isPR bool, err error) { + if c == nil || c.client == nil { + return "", "", false, ErrNoGitHubClient + } + issue, _, err := c.client.Issues.Get(ctx, owner, repo, number) + if err != nil { + return "", "", false, err + } + if issue.IsPullRequest() { + return "", "", true, nil + } + return issue.GetTitle(), strings.ToLower(issue.GetState()), false, nil +} diff --git a/src/pkg/github/pr_reference_links.go b/src/pkg/github/pr_reference_links.go index 8f236a090a..659b134b5e 100644 --- a/src/pkg/github/pr_reference_links.go +++ b/src/pkg/github/pr_reference_links.go @@ -56,7 +56,8 @@ var prTitleReferencePattern = regexp.MustCompile(`\((?:([\w.-]+/[\w.-]+))?#(\d+) // without GitHub having derived a closing link, in priority order: closing // keywords in the text (PRLinkViaFixes), reference keywords in the body // (PRLinkViaRefs), parenthesised references in the title (PRLinkViaTitle), -// then bare `#N` mentions in the body's first paragraph (PRLinkViaMention). +// then bare `#N` mentions in the body's first paragraph (PRLinkViaMention) +// unless the text says the PR does not cover them (prMentionUncovered). // Only same-repo references are kept — a bare `#N`, or `owner/repo#N` // naming repo itself — never the PR's own number, each number once at its // strongest tier. Title and State are left empty: the dashboard fills them @@ -92,16 +93,53 @@ func PRReferencedIssues(title, body, repo string, prNumber int) []PRLinkedIssue } add(m[1], n, PRLinkViaTitle) } - for _, m := range issueRefPattern.FindAllStringSubmatch(firstParagraph(cleanBody), -1) { - n, err := strconv.Atoi(m[2]) + lead := firstParagraph(cleanBody) + for _, m := range issueRefPattern.FindAllStringSubmatchIndex(lead, -1) { + if prMentionUncovered(lead, m[0], m[1]) { + continue + } + n, err := strconv.Atoi(lead[m[4]:m[5]]) if err != nil { continue } - add(m[1], n, PRLinkViaMention) + refRepo := "" + if m[2] >= 0 { + refRepo = lead[m[2]:m[3]] + } + add(refRepo, n, PRLinkViaMention) } return out } +// prMentionUncoveredPattern is prose after a bare `#N` saying the PR leaves +// that issue alone: "#14559 backend-specific routes remain", "#7 is out of +// scope". +var prMentionUncoveredPattern = regexp.MustCompile(`(?i)\b(?:remains?|remaining|left (?:open|for|to)|not covered|out of scope|not in this pr)\b`) + +// prMentionQualifiedPattern is text ending in a reference, so a parenthetical +// opening right after it qualifies that reference. +var prMentionQualifiedPattern = regexp.MustCompile(`#\d+[ \t]*$`) + +// prMentionUncovered reports whether the bare `#N` at text[start:end] names +// work the PR does not cover (hivecommons/hive#14667): its clause, up to the +// next , ; . ) or line end, says the issue remains, or it sits inside a +// parenthetical qualifying the reference just before it, as in +// `Part of #14550 (tracker; #14559 backend-specific routes remain)`. +func prMentionUncovered(text string, start, end int) bool { + clause := text[end:] + if i := strings.IndexAny(clause, ",;.)\n"); i >= 0 { + clause = clause[:i] + } + if prMentionUncoveredPattern.MatchString(clause) { + return true + } + open := strings.LastIndexByte(text[:start], '(') + if open < 0 || strings.ContainsRune(text[open:start], ')') { + return false + } + return prMentionQualifiedPattern.MatchString(text[:open]) +} + // firstParagraph is text up to its first blank line. func firstParagraph(text string) string { text = strings.TrimLeft(strings.ReplaceAll(text, "\r\n", "\n"), "\n") diff --git a/src/pkg/github/pr_reference_links_test.go b/src/pkg/github/pr_reference_links_test.go index 6839bb1224..c9deefd625 100644 --- a/src/pkg/github/pr_reference_links_test.go +++ b/src/pkg/github/pr_reference_links_test.go @@ -57,6 +57,18 @@ func TestPRReferencedIssues(t *testing.T) { body: "Follow-up to #40 after the rename.\n\nAlso touches #41 in passing.", want: []PRLinkedIssue{{Number: 40, Repo: repo, Via: PRLinkViaMention}}, }, + { + // hivecommons/hive#14667: #14559 is the sibling the PR leaves + // for later, named inside the tracker reference's parenthetical. + name: "mention qualifying another ref or said to remain does not bind", + body: "Closes #14558\nPart of #14550 (tracker; #14559 backend-specific routes remain)", + want: []PRLinkedIssue{{Number: 14558, Repo: repo, Via: PRLinkViaFixes}, {Number: 14550, Repo: repo, Via: PRLinkViaRefs}}, + }, + { + name: "mention said to remain or qualifying another mention does not bind", + body: "Builds on #30; #31 remains open. Touches #32 (and #33).", + want: []PRLinkedIssue{{Number: 30, Repo: repo, Via: PRLinkViaMention}, {Number: 32, Repo: repo, Via: PRLinkViaMention}}, + }, { name: "cross-repo refs ignored, same-repo explicit kept", body: "Refs acme/other#9 and hivecommons/hive#10",