From 5e9fd1ec8c05de3727afbd8ce75974800009f6f6 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Thu, 5 Nov 2026 11:26:22 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(dashboard):=20Let=20hive=20d?= =?UTF-8?q?ecide=20preview=20says=20why=20it=20failed=20instead=20of=20"hi?= =?UTF-8?q?ve=20unavailable"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The preview answered every failure as 502 — a PR merged since the row loaded, an intermittent GitHub 500 on check-runs — and the hosted Ingress intercepts 502/503 and swaps the body for its generic outage page, so the owner read "hive unavailable" for a hive that was up. - retry transient GitHub failures twice with backoff before giving up - answer not-open as 409 and unrecoverable reads as 424, never 502/503, with {reason, retryable, attempts, elapsed_ms, hive, hint} - log a Warn with the real error on every failed plan - the dialog names the hive, explains the failure (merged since the row loaded / GitHub failed after N attempts / no GitHub client / gateway 502), hides Retry when it cannot help, and says Let hive do that stays off until a preview loads Refs hivecommons/hive#16776 Signed-off-by: Andrew Anderson --- .../fixed-decide-plan-hive-unavailable.md | 1 + src/docs/api-reference/packs-and-acmm.md | 2 +- .../dashboard-sections/activity-monitor.md | 2 +- .../api_repo_item_decide_delegated.go | 2 +- .../api_repo_item_decide_delegated_plan.go | 147 +++++++++++++++++- ...pi_repo_item_decide_delegated_plan_test.go | 93 +++++++++++ .../dashboard/api_repo_item_decision_test.go | 12 ++ .../stage_tracker_decide_plan_cache_test.go | 30 +++- .../stage_tracker_row_actions_test.go | 6 + src/pkg/dashboard/static-src/css/tracker.css | 2 + .../static-src/js/tracker-actions.js | 86 +++++++++- src/pkg/dashboard/static/index.html | 88 ++++++++++- 12 files changed, 445 insertions(+), 26 deletions(-) create mode 100644 changelog.d/fixed-decide-plan-hive-unavailable.md diff --git a/changelog.d/fixed-decide-plan-hive-unavailable.md b/changelog.d/fixed-decide-plan-hive-unavailable.md new file mode 100644 index 0000000000..bbb337918e --- /dev/null +++ b/changelog.d/fixed-decide-plan-hive-unavailable.md @@ -0,0 +1 @@ +- "Let hive decide" no longer fails with a bare "hive unavailable": the preview retries transient GitHub failures (an intermittent 500 on check-runs failed it outright), answers a merged/closed item or an unrecoverable GitHub read as 409/424 with the reason instead of a 502 the hosted Ingress replaced with its generic outage page, and the dialog now names the hive, says why it could not preview (merged since the row loaded, GitHub failed after N attempts, no GitHub client, gateway 502) and that "Let hive do that" stays off until a preview loads (hivecommons/hive#16776). diff --git a/src/docs/api-reference/packs-and-acmm.md b/src/docs/api-reference/packs-and-acmm.md index d5d1e1a8c4..171e3433cc 100644 --- a/src/docs/api-reference/packs-and-acmm.md +++ b/src/docs/api-reference/packs-and-acmm.md @@ -19,7 +19,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `POST` | `/api/repos/{owner}/{repo}/items/{number}/hold` | Owner or repo write/maintain/admin | Add or remove a dashboard hold chip for an issue or PR. Body `{"held": true}` adds the hive's exact-match canonical `hive-pause/` label; `{"held": false}` removes exactly the labels causing the hold (`hive-pause/` and/or generic hold labels) and never removes `hive/` provenance. Every toggle writes an audit-log entry; the #8851 upgrade migration writes `/data/hive-hold-migration-.json` with audit-backed, provenance-only, and ambiguous legacy `hive/` classifications. | `pkg/dashboard/api.go:345`, `pkg/dashboard/api_repo_hold.go` | | `POST` | `/api/repos/{owner}/{repo}/items/{number}/decision` | Owner or repo write/maintain/admin, signed-in GitHub user | Record an operator decision from the activity monitor's row menu / selection bar. Body `{"decision": "approve"|"direction"|"close-superseded"|"close-wontfix"|"close-read"|"close-issue"|"adopt"|"reroute"|"re-author"|"merge-now"|"queue-merge", "comment": "…", "type": "issue"|"pr", "superseded_by": "#458", "force": true, "to": ""|"any"}` (`force` only with `adopt`, `to` only with `reroute`; an unknown `decision` is a 400). On an issue parked at 045, `approve`/`direction` act exactly as a maintainer's `/hive approve` / `/hive decision ` comment would (labels, `approved-direction`, stage-engine Unparked fact, reply comment naming the operator). On a PR parked at 085 (`type: "pr"`), `approve` posts the `/lgtm` and `/approve` commands as two comments attributed to the operator, records the approval against the head as the human answer to a `requires_human` / decision-requested verdict (the lanes and the stage engine honour it like a trusted merger's Approve review), lifts `needs-human`, clears the sentinel label and acknowledges the bundle's sentinel findings for that head, re-derives the stage and drops the tracker cache — the response adds `released` (`approval-recorded`, `needs-human`, `sentinel-label`, `sentinel-acknowledged`) and `stage`; the merge gate decides from there (no review as the operator, no merge) — and `direction` records the text as a human change request against the head — the `/hive decision` fix lane — on a hive-authored PR, and on a contributor's clanker PR (`author/clanker` / hive attribution markers) where the text is posted to the contributor's hive and the PR moves to `stage/072-returned-to-contributor` (`status: "direction-returned-to-contributor"`); a person's PR, which no hive can carry direction to, answers 409. The close variants first post a required comment naming the operator (and the superseding PR when `superseded_by` is given); a failed comment prevents closure. `close-superseded` and `close-wontfix` close as **not planned**; `close-read` acknowledges an advisory issue and closes it **as completed**. `close-issue` (`type: "issue"` only) closes the single named open issue **as completed**, at any stage, leaving linked PRs and other issues untouched; it returns 400 for a PR (including one sent as an issue), 404 when the issue is not found, and 409 when already closed. It retains the existing permission and mutation gates, including the paused-repo `status: "refused"` result without writes. `adopt` and `re-author` require `type: "pr"` and queue adoption via the `adoption/supersede` label (409 unless `governor.labels.stage.adoption` is enabled). `adopt` with `"force": true` is *Adopt now* ([#15038](https://github.com/hivecommons/hive/issues/15038)): it bypasses the author's presence, the grace window and `maintainer_policy`, marks the stage record and nudges the automerge sweep so the adoption engine supersedes the head at once (responds `status: "already-adopted"` when nothing is left to do; 400 on the hive's own PR). `reroute` (`type: "pr"`, `"to": ""|"any"`) hands the PR's follow-up work to another online clanker through the contributor fix queue, posts a `` note and arms a pickup timer that falls back to Adopt now after `governor.reroute.pickup_timeout`; answers `status: "rerouted"`, 409 when no clanker is online or adoption is disabled. On a PR at 090, `queue-merge` (*Approve and let hive merge*, the lead action) records an approval attributed to the operator and applies the automerge label, returning `status: "queued-for-merge"` (on the hive's own PR, which the App cannot review, the queue note is a comment and the operator's approval is recorded as `approve` records it — `released` lists `approval-recorded`, `admitted`); hive's merge sweep merges on its next tick once CI is green and the head has soaked, and that recorded operator approval is the one thing that lets the sweep merge an untrusted-author PR (the merge comment names the operator). `merge-now` (*Merge now as maintainer*, secondary) posts an attributed comment and merges immediately through the hive's GitHub App, returning `status: "merged"`. Both require `type: "pr"` and a verified owner or `merger` role in addition to the route's repo permission check; both refuse closed/merged PRs and the operator's own PR. `merge-now` on a PR whose `mergeable_state` is `dirty`/`behind` instead answers `202` with `status: "rebase-queued"` (approval recorded, rebase kicked, merge follows via the follow-through once the hive's own rebase is green; a head pushed by anyone else fails the follow-through closed; `head-not-pushable` refusal on a fork without maintainer edits). `approve` on such a PR adds `rebase: {attempt, max, exhausted, head, text}` for the rebase episode it opened. Every decision writes an audit-log entry (`repo_item_decision_*`, e.g. `repo_item_decision_adopt_now`, `repo_item_decision_reroute`). | `pkg/dashboard/api.go:346`, `pkg/dashboard/api_repo_item_decision.go` | | `POST` / `GET` | `/api/repos/{owner}/{repo}/items/{number}/decide-delegated` | Same as `…/hold` (owner or repo write), signed-in GitHub user; `decisions.delegate.enabled` (403 when off) | *Let hive decide*: enqueue a delegated-decision job in which the hive decides the parked item itself as the owner's delegate under `pkg/delegatedecision.PolicyText` and records it as a human decision (`/hive decision` comment with the `` marker, park labels off, `triage/accepted`, queue approval on a PR head). Body `{"type": "issue"|"pr", "reason", "caption", "sentinel_reason"}`. `POST` answers 202 with the job (`{id, status: "running"|"done"|"failed", decision: {verdict, rule, text, rationale, actions, keeps_park, ladder?: {decision, reason, ruled_out[]}}, notes, error}`; `ladder` is set on contributor-PR decisions and names the rung that decided — `already-fixed` → `mergeable-as-is` → `rebase-then-merge` → `return-to-contributor` → `contributor-offline` — and why each rung above it did not apply); 409 with the running job while one is in flight; `GET` reads it. Rails: never merges, refuses items wearing `hold`, never grants secrets/permissions (`needs-owner` + `hold` instead), recommends only on hive policy/governance questions (keeps 085). Audited as `repo_item_decision_delegated[_failed]`. See [dashboard-sections.md](../dashboard-sections.md#let-hive-decide). | `pkg/dashboard/api.go:329`, `pkg/dashboard/api_repo_item_decide_delegated.go` | -| `POST` | `/api/repos/{owner}/{repo}/items/{number}/decide-delegated/plan` | Same as `…/decide-delegated` | Preview for *Let hive decide*: runs the same owner-delegate policy with **no side effects** (nothing written, no job) and answers `{would, rule, acting, title, summary, why, consequences[], ladder, mergeable_as_is, alternatives[], writes: false, prior, notes}` — `summary` is one verb-first sentence with no "Hive would:" prefix (*Send #15332 back to its author with one instruction: make CI green, nothing else.*), `why` one short line (*CI is red.*), `consequences` at most two items, `ladder` the rung that decided with its `ruled_out` list, and `mergeable_as_is` whether an as-is merge is honest (the queue-merge and merge-now alternatives are dropped when it is not). The dialog shows **Cancel** / **Let hive do that**, plus **Approve and let hive merge** (or **Merge now as maintainer** when the hive would approve anyway) only when `mergeable_as_is`; only then does the SPA post `…/decide-delegated` with `chosen` naming the button. See [dashboard-sections.md](../dashboard-sections.md#let-hive-decide). | `pkg/dashboard/api.go:349`, `pkg/dashboard/api_repo_item_decide_delegated_plan.go` | +| `POST` | `/api/repos/{owner}/{repo}/items/{number}/decide-delegated/plan` | Same as `…/decide-delegated` | Preview for *Let hive decide*: runs the same owner-delegate policy with **no side effects** (nothing written, no job) and answers `{would, rule, acting, title, summary, why, consequences[], ladder, mergeable_as_is, alternatives[], writes: false, prior, notes}` — `summary` is one verb-first sentence with no "Hive would:" prefix (*Send #15332 back to its author with one instruction: make CI green, nothing else.*), `why` one short line (*CI is red.*), `consequences` at most two items, `ladder` the rung that decided with its `ruled_out` list, and `mergeable_as_is` whether an as-is merge is honest (the queue-merge and merge-now alternatives are dropped when it is not). The dialog shows **Cancel** / **Let hive do that**, plus **Approve and let hive merge** (or **Merge now as maintainer** when the hive would approve anyway) only when `mergeable_as_is`; only then does the SPA post `…/decide-delegated` with `chosen` naming the button. A transient GitHub failure (5xx, rate limit, timeout) is retried twice with backoff before the plan gives up. A failure answers `{ok: false, error, reason, retryable, attempts, elapsed_ms, hive, hint}` under **409** when the item is no longer open (`reason: not-open`) or **424** when a read it depends on failed (`github`), is not configured (`config`) or the plan itself failed (`plan`) — never 502/503, which the hosted Ingress intercepts and replaces with its generic *hive unavailable* page (hivecommons/hive#16776). See [dashboard-sections.md](../dashboard-sections.md#let-hive-decide). | `pkg/dashboard/api.go:349`, `pkg/dashboard/api_repo_item_decide_delegated_plan.go` | | `POST` | `/api/repos/{owner}/{repo}/items/{number}/blocker/recheck` | Same as `…/hold` (owner or repo write), signed-in GitHub user | *Re-check permissions* on an issue parked on a capability blocker (#15099): probes the GitHub App installation's permissions; when the missing permission is granted, clears every ledger entry blocked on it and un-parks the item (`{result: {outcome: "released", …}}`), otherwise `{result: {outcome: "no-effect", reason: "blocker", options: [...]}}`; 404 when the item has no blocker, 409 when the blocker is not a capability gap | `pkg/dashboard/api.go:350` | | `POST` | `/api/repos/{owner}/{repo}/items/{number}/blocker/close-partial` | Same as `…/hold` (owner or repo write), signed-in GitHub user | Option (c) on a capability blocker (#15099): labels the issue `hive/partially-addressed`, posts the operator's `{"comment"}` naming what remains and closes it; the ledger entry is cleared | `pkg/dashboard/api.go:351` | | `POST` | `/api/repos/{owner}/{repo}/items/{number}/accept-triage` | Owner or repo write/maintain/admin | Accept triage on an issue from the activity monitor's row menu — what a maintainer accepting triage on GitHub does by hand: adds the reporter-trust triage label (`triage/accepted`, or the first of `issue_filter.reporter_trust.untrusted_require_labels`; the label is created in the repo if missing) and removes the awaiting label (`needs-triage` / `reporter_trust.awaiting_label`) and a Hive-written `stage/015-triage-needed`. Empty body. Returns `{ok, repo, number, acceptLabel, added, removed, alreadyAccepted, minStatusSeq}`; an issue already carrying the accept label with nothing left to remove is a no-op (`alreadyAccepted: true`, no write, no audit). 404 for a pull request or an unknown item. The status snapshot's labels are patched so the next reconcile reads 010 Actionable before the engine's own 015 → 010 derivation. Every write is audited as `repo_item_accept_triage` under the dashboard user. | `pkg/dashboard/api.go:546`, `pkg/dashboard/api_repo_item_accept_triage.go` | diff --git a/src/docs/dashboard-sections/activity-monitor.md b/src/docs/dashboard-sections/activity-monitor.md index e82456c18e..63e1551a9b 100644 --- a/src/docs/dashboard-sections/activity-monitor.md +++ b/src/docs/dashboard-sections/activity-monitor.md @@ -672,7 +672,7 @@ The item leaves 085/045 and proceeds without the owner. The action is audited as #### How it runs -1. The preview opens at once as a skeleton: the title, the item, **Asking the hive…** and placeholder lines (still under *prefers-reduced-motion*). **Cancel** works straight away; **Let hive do that** stays disabled until the plan lands and fills the dialog. Until then the row's *Let hive decide* button and its caption read **Asking the hive…** too, and a second click (button, ⋯ entry or popover) opens nothing. If the plan fails, the dialog says so inline with **Retry** — no toast, no browser alert. +1. The preview opens at once as a skeleton: the title, the item, **Asking the hive…** and placeholder lines (still under *prefers-reduced-motion*). **Cancel** works straight away; **Let hive do that** stays disabled until the plan lands and fills the dialog. Until then the row's *Let hive decide* button and its caption read **Asking the hive…** too, and a second click (button, ⋯ entry or popover) opens nothing. If the plan fails, the dialog says so inline — no toast, no browser alert — and says *which* hive could not preview and *why*, in that hive's own words: the item is no longer open (merged or closed after the row loaded — refresh, nothing to decide; no Retry), GitHub failed a read the preview needs after the hive's own retries (how many, over how long — **Retry**), the hive has no GitHub client configured, or the hosted gateway answered 502/503 in the hive's place (restarting — retry in a few seconds). The last line always says that **Let hive do that** stays off until a preview loads: the hive never records a decision it could not preview (hivecommons/hive#16776, where every one of those read as a bare *hive unavailable*). 2. Read the one-time explanation and confirmation. **Don't ask again for this action** is optional; reset it in ⚙ → **Confirmations**. 3. The row reads **Hive deciding…** while the job runs. 4. Read the persistent result toast. It says what was decided and what happens next; it stays until the target row changes state. diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated.go b/src/pkg/dashboard/api_repo_item_decide_delegated.go index d0c527bf1a..ea5500c9b4 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated.go @@ -634,7 +634,7 @@ func (s *Server) gatherDelegatedInput(ctx context.Context, repoFull string, numb return in, fmt.Errorf("reading %s#%d: %w", repoFull, number, err) } if issue.GetState() != "" && !strings.EqualFold(issue.GetState(), "open") { - return in, fmt.Errorf("%s#%d is not open", repoFull, number) + return in, &delegatedItemNotOpenError{Ref: fmt.Sprintf("%s#%d", repoFull, number), State: strings.ToLower(issue.GetState())} } in.Title = issue.GetTitle() in.Body = issue.GetBody() diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated_plan.go b/src/pkg/dashboard/api_repo_item_decide_delegated_plan.go index 76c32c7319..3a2c015aec 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated_plan.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated_plan.go @@ -13,8 +13,11 @@ import ( "sync" "time" + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/delegatedecision" + ghpkg "github.com/hivecommons/hive/pkg/github" ) // The "Let hive decide" preview (hivecommons/hive#15126). The owner pressed @@ -89,7 +92,8 @@ func (s *Server) handleRepoItemDecideDelegatedPlan(w http.ResponseWriter, r *htt return } if s.deps == nil || s.deps.GHClient == nil || s.deps.GHClient.GoGitHub() == nil { - jsonError(w, "GitHub client not configured", http.StatusServiceUnavailable) + failure := delegatedPlanFailureFor(errDelegatedPlanNoGitHub, repoFull, number, 0, 0, s.publicHiveName()) + jsonStatusResponse(w, failure.status, failure) return } var body delegatedDecisionRequest @@ -118,9 +122,14 @@ func (s *Server) handleRepoItemDecideDelegatedPlan(w http.ResponseWriter, r *htt if !ok { cache = "miss" var err error - plan, err = s.planDelegatedDecision(ctx, repoFull, number, itemType, mergerRole, body) + var attempts int + plan, attempts, err = s.planDelegatedDecisionWithRetry(ctx, repoFull, number, itemType, mergerRole, body) if err != nil { - jsonError(w, err.Error(), http.StatusBadGateway) + failure := delegatedPlanFailureFor(err, repoFull, number, attempts, time.Since(start), s.publicHiveName()) + if s.logger != nil { + s.logger.Warn("decide-delegated plan failed", "repo", repoFull, "number", number, "type", itemType, "reason", failure.Reason, "attempts", attempts, "retryable", failure.Retryable, "status", failure.status, "duration_ms", time.Since(start).Milliseconds(), "error", err) + } + jsonStatusResponse(w, failure.status, failure) return } s.delegatedJobs.plans.put(key, plan, time.Now()) @@ -141,8 +150,140 @@ const ( delegatedPlanCacheMax = 256 // delegatedPlanPrefetchHeader marks the SPA's prefetch (log only). delegatedPlanPrefetchHeader = "X-Hive-Prefetch" + // delegatedPlanRetries is how many more times the plan re-reads the + // item after a transient GitHub failure (5xx, rate limit, timeout) + // before the preview gives up; delegatedPlanRetryBackoff is the pause + // before the first retry, doubled for the next. The plan is a handful + // of sequential GitHub reads and api.github.com answers one of them + // with an intermittent 500 often enough (check-runs on a 70-check head, + // observed live 2026-11-05) that a single attempt failed the preview + // for the owner several times a day. + delegatedPlanRetries = 2 + delegatedPlanRetryBackoff = 750 * time.Millisecond ) +// delegatedPlanRetrySleep is the retry pause; tests replace it. +var delegatedPlanRetrySleep = func(ctx context.Context, d time.Duration) error { + t := time.NewTimer(d) + defer t.Stop() + select { + case <-ctx.Done(): + return ctx.Err() + case <-t.C: + return nil + } +} + +// planDelegatedDecisionWithRetry is planDelegatedDecision retried on a +// transient GitHub failure. It returns the plan, how many attempts ran and +// the last error. +func (s *Server) planDelegatedDecisionWithRetry(ctx context.Context, repoFull string, number int, itemType string, mergerRole bool, req delegatedDecisionRequest) (*delegatedDecisionPlan, int, error) { + backoff := delegatedPlanRetryBackoff + for attempt := 1; ; attempt++ { + plan, err := s.planDelegatedDecision(ctx, repoFull, number, itemType, mergerRole, req) + if err == nil { + return plan, attempt, nil + } + if attempt > delegatedPlanRetries || !ghpkg.IsRetryableError(err) { + return nil, attempt, err + } + if s.logger != nil { + s.logger.Debug("decide-delegated plan: transient GitHub failure, retrying", "repo", repoFull, "number", number, "attempt", attempt, "error", err) + } + if serr := delegatedPlanRetrySleep(ctx, backoff); serr != nil { + return nil, attempt, err + } + backoff *= 2 + } +} + +// delegatedItemNotOpenError is gatherDelegatedInput's answer for an item +// GitHub reports closed or merged: there is nothing to decide, and the +// row the operator clicked is simply older than the merge. +type delegatedItemNotOpenError struct { + Ref string + State string +} + +func (e *delegatedItemNotOpenError) Error() string { return e.Ref + " is not open" } + +// errDelegatedPlanNoGitHub is the plan's answer on a hive with no GitHub +// client: it can neither preview nor decide. +var errDelegatedPlanNoGitHub = errors.New("GitHub client not configured") + +// The plan's failure reasons, keyed off by the SPA's inline error +// (tracker-actions.js stageTrackerDelegatePlanFailureLines). +const ( + delegatedPlanReasonNotOpen = "not-open" + delegatedPlanReasonGitHub = "github" + delegatedPlanReasonConfig = "config" + delegatedPlanReasonPlan = "plan" +) + +// delegatedPlanFailure is the wire shape of a failed POST +// …/decide-delegated/plan: ok:false and error as every dashboard error +// carries, plus what the SPA needs to say something actionable — which +// hive answered, why it could not preview, whether Retry is worth +// pressing, and how many GitHub attempts over how long it already made. +// +// Its status is never 502 or 503: the Ingress in front of a hosted hive +// intercepts both (custom-http-errors "401,502,503", saas_provision.go) +// and replaces this body with the generic "hive unavailable" page, which +// is how a merged PR and an intermittent GitHub 500 both read as "the hive +// is down" for weeks (the same trap as #16652's SSO 503). A failure the +// operator can act on is a 4xx: 409 when the item is no longer open, 424 +// when a read the plan depends on (GitHub) failed or is not configured. +type delegatedPlanFailure struct { + OK bool `json:"ok"` + Error string `json:"error"` + Reason string `json:"reason"` + Retryable bool `json:"retryable"` + Attempts int `json:"attempts,omitempty"` + ElapsedMS int64 `json:"elapsed_ms,omitempty"` + Hive string `json:"hive,omitempty"` + Hint string `json:"hint"` + status int +} + +// delegatedPlanFailureFor classifies a plan error into the response the +// SPA renders. hive is the display name of the hive that answered. +func delegatedPlanFailureFor(err error, repoFull string, number int, attempts int, elapsed time.Duration, hive string) delegatedPlanFailure { + ref := fmt.Sprintf("%s#%d", repoFull, number) + f := delegatedPlanFailure{Error: err.Error(), Reason: delegatedPlanReasonPlan, Attempts: attempts, ElapsedMS: elapsed.Milliseconds(), Hive: hive, status: http.StatusFailedDependency} + var notOpen *delegatedItemNotOpenError + switch { + case errors.As(err, ¬Open): + f.Reason = delegatedPlanReasonNotOpen + f.status = http.StatusConflict + state := notOpen.State + if state == "" { + state = "closed" + } + f.Hint = fmt.Sprintf("%s is %s on GitHub — it was merged or closed after this row loaded, so there is nothing left to decide. Refresh the activity monitor.", ref, state) + case errors.Is(err, errDelegatedPlanNoGitHub): + f.Reason = delegatedPlanReasonConfig + f.Hint = "This hive has no GitHub client configured, so it can neither preview nor record a decision. The hive operator needs to finish the GitHub App setup." + case ghpkg.IsRetryableError(err): + f.Reason = delegatedPlanReasonGitHub + f.Retryable = true + f.Hint = fmt.Sprintf("GitHub's API failed a read the preview needs on each of %d attempts over %.0fs; this is usually transient on GitHub's side — Retry.", attempts, elapsed.Seconds()) + case isGitHubAPIError(err): + f.Reason = delegatedPlanReasonGitHub + f.Hint = fmt.Sprintf("GitHub refused a read the preview of %s needs. Check the hive's GitHub App access to %s.", ref, repoFull) + default: + f.Hint = "The hive could not build the preview from what GitHub returned; Retry once, then report it if it persists." + f.Retryable = true + } + return f +} + +// isGitHubAPIError reports whether err wraps a GitHub API error response +// of any status (the retryable 5xx/429 are classified first). +func isGitHubAPIError(err error) bool { + var ghErr *gh.ErrorResponse + return errors.As(err, &ghErr) +} + // delegatedPlanServerTiming is the plan's Server-Timing metric: its // duration in milliseconds and whether the cache answered. func delegatedPlanServerTiming(cache string, d time.Duration) string { diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated_plan_test.go b/src/pkg/dashboard/api_repo_item_decide_delegated_plan_test.go index 2094a052b4..5f8642ff4c 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated_plan_test.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated_plan_test.go @@ -1,14 +1,20 @@ package dashboard import ( + "context" "encoding/json" + "errors" + "fmt" "net/http" "net/http/httptest" + "net/url" "strconv" "strings" "testing" "time" + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/delegatedecision" "github.com/hivecommons/hive/pkg/stageengine" "github.com/hivecommons/hive/pkg/stagelabels" @@ -440,3 +446,90 @@ func TestDelegatedPlanPendingCIIsNotRed(t *testing.T) { t.Fatal("a head with checks still running is not mergeable as it is") } } + +// hivecommons/hive#16776: the owner's "Let hive decide" preview failed with +// "hive unavailable" several times a day. The plan answered every failure +// — a PR merged since the row loaded, an intermittent GitHub 500 on +// check-runs — as 502, and the hosted Ingress intercepts 502/503 and swaps +// the body for its generic outage page. The plan now retries a transient +// GitHub failure, and a failure it cannot recover from is a 4xx carrying +// the reason, so the dialog can say what happened and what to do. +func TestDelegatedPlanRetriesTransientGitHubFailure(t *testing.T) { + prev := delegatedPlanRetrySleep + var slept []time.Duration + delegatedPlanRetrySleep = func(_ context.Context, d time.Duration) error { slept = append(slept, d); return nil } + t.Cleanup(func() { delegatedPlanRetrySleep = prev }) + + mock := &repoItemDecisionMock{prForeign: true, prRedCI: true, prMergeState: "blocked", prLabels: []string{"stage/085-needs-decision", "needs-human"}, checkRunsFailures: 1} + srv := newDelegatedServer(t, mock) + w := postDelegatedPlan(t, srv, 8, "writer", `{"type":"pr"}`, map[string]string{"X-Hive-Role": "merger"}) + if w.Code != http.StatusOK { + t.Fatalf("one GitHub 500 must be retried, got code=%d body=%s", w.Code, w.Body.String()) + } + if len(slept) != 1 || slept[0] != delegatedPlanRetryBackoff || mock.checkRunsCalls != 2 { + t.Fatalf("slept=%v check-runs calls=%d", slept, mock.checkRunsCalls) + } + + // Past the retry budget the failure is reported — as 424, never 502, + // with the reason, the attempt count and a hint. + slept = nil + mock2 := &repoItemDecisionMock{prForeign: true, prRedCI: true, prMergeState: "blocked", prLabels: []string{"stage/085-needs-decision", "needs-human"}, checkRunsFailures: delegatedPlanRetries + 5} + srv2 := newDelegatedServer(t, mock2) + w = postDelegatedPlan(t, srv2, 8, "writer", `{"type":"pr"}`, map[string]string{"X-Hive-Role": "merger"}) + if w.Code != http.StatusFailedDependency { + t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) + } + var f delegatedPlanFailure + if err := json.Unmarshal(w.Body.Bytes(), &f); err != nil { + t.Fatal(err) + } + if f.OK || f.Reason != delegatedPlanReasonGitHub || !f.Retryable || f.Attempts != delegatedPlanRetries+1 || !strings.Contains(f.Error, "check-runs") || !strings.Contains(f.Hint, "Retry") { + t.Fatalf("failure = %+v", f) + } + if len(slept) != delegatedPlanRetries || slept[1] != 2*delegatedPlanRetryBackoff { + t.Fatalf("slept=%v", slept) + } +} + +func TestDelegatedPlanNotOpenIsConflictNotBadGateway(t *testing.T) { + mock := &repoItemDecisionMock{issueState: "closed", issueLabels: []string{"stage/045-needs-decision"}} + srv := newDelegatedServer(t, mock) + w := postDelegatedPlan(t, srv, 7, "writer", `{"type":"issue"}`, nil) + if w.Code != http.StatusConflict { + t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) + } + var f delegatedPlanFailure + if err := json.Unmarshal(w.Body.Bytes(), &f); err != nil { + t.Fatal(err) + } + if f.OK || f.Reason != delegatedPlanReasonNotOpen || f.Retryable || f.Error != "testorg/testrepo#7 is not open" || !strings.Contains(f.Hint, "closed on GitHub") || !strings.Contains(f.Hint, "Refresh") { + t.Fatalf("failure = %+v", f) + } +} + +// delegatedPlanFailureFor never picks a status the hosted Ingress +// intercepts (502, 503): that is how the real reason went missing. +func TestDelegatedPlanFailureStatusesAreNeverIntercepted(t *testing.T) { + ghErr := &gh.ErrorResponse{Response: &http.Response{StatusCode: http.StatusInternalServerError, Request: &http.Request{Method: "GET", URL: &url.URL{Path: "/x"}}}} + cases := []struct { + err error + status int + reason string + retryable bool + }{ + {&delegatedItemNotOpenError{Ref: "o/r#1", State: "closed"}, http.StatusConflict, delegatedPlanReasonNotOpen, false}, + {errDelegatedPlanNoGitHub, http.StatusFailedDependency, delegatedPlanReasonConfig, false}, + {fmt.Errorf("reading checks: %w", ghErr), http.StatusFailedDependency, delegatedPlanReasonGitHub, true}, + {fmt.Errorf("reading: %w", &gh.ErrorResponse{Response: &http.Response{StatusCode: http.StatusNotFound, Request: &http.Request{Method: "GET", URL: &url.URL{Path: "/x"}}}}), http.StatusFailedDependency, delegatedPlanReasonGitHub, false}, + {errors.New("something else"), http.StatusFailedDependency, delegatedPlanReasonPlan, true}, + } + for _, tc := range cases { + f := delegatedPlanFailureFor(tc.err, "o/r", 1, 3, 4*time.Second, "Hive Commons") + if f.status == http.StatusBadGateway || f.status == http.StatusServiceUnavailable { + t.Errorf("%v: status %d is intercepted by the hosted Ingress", tc.err, f.status) + } + if f.status != tc.status || f.Reason != tc.reason || f.Retryable != tc.retryable || f.Hive != "Hive Commons" || f.Hint == "" || f.OK { + t.Errorf("%v: failure = %+v", tc.err, f) + } + } +} diff --git a/src/pkg/dashboard/api_repo_item_decision_test.go b/src/pkg/dashboard/api_repo_item_decision_test.go index d323f1d2fd..84e3a94dcb 100644 --- a/src/pkg/dashboard/api_repo_item_decision_test.go +++ b/src/pkg/dashboard/api_repo_item_decision_test.go @@ -81,6 +81,11 @@ type repoItemDecisionMock struct { commentStarted chan struct{} // editPaths records each PATCH path alongside edits. editPaths []string + // checkRunsFailures is how many GET /check-runs answer 500 before the + // mock recovers (api.github.com's intermittent 500 on a long head); + // checkRunsCalls counts every GET /check-runs. + checkRunsFailures int + checkRunsCalls int } func newRepoItemDecisionServer(t *testing.T, m *repoItemDecisionMock) *Server { @@ -202,6 +207,13 @@ func newRepoItemDecisionServer(t *testing.T, m *repoItemDecisionMock) *Server { _, _ = io.WriteString(w, `{"state":"success","statuses":[{"context":"ci/test","state":"success"}]}`) } case r.Method == http.MethodGet && strings.HasSuffix(path, "/commits/feedfacefeedfacefeedfacefeedfacefeedface/check-runs"): + m.checkRunsCalls++ + if m.checkRunsFailures > 0 { + m.checkRunsFailures-- + w.WriteHeader(http.StatusInternalServerError) + _, _ = io.WriteString(w, `[]`) + return + } _, _ = io.WriteString(w, `{"total_count":0,"check_runs":[]}`) case r.Method == http.MethodGet && strings.Contains(path, "/commits/") && !strings.HasSuffix(path, "/status") && !strings.HasSuffix(path, "/check-runs"): sha := path[strings.LastIndex(path, "/")+1:] diff --git a/src/pkg/dashboard/stage_tracker_decide_plan_cache_test.go b/src/pkg/dashboard/stage_tracker_decide_plan_cache_test.go index aedc2a075a..7fa36e0f5f 100644 --- a/src/pkg/dashboard/stage_tracker_decide_plan_cache_test.go +++ b/src/pkg/dashboard/stage_tracker_decide_plan_cache_test.go @@ -250,8 +250,30 @@ func TestStageTrackerDelegatePreviewSkeletonPrefetch(t *testing.T) { assert.ok(/]*class="[^"]*cancel"[^>]*data-initial-focus>Cancel<\/button>/.test(sk) && !/cancel"[^>]*disabled/.test(sk), 'Cancel is enabled and focused: ' + sk); assert.ok(/]*primary" disabled aria-disabled="true">Let hive do that<\/button>/.test(sk), 'Let hive do that waits for the plan: ' + sk); assert.ok(/ham-plan-merge" hidden>/.test(sk), sk); - const err = stageTrackerDelegatePlanErrorContent('o/r#4', [{ ref: 'o/r#4', error: 'HTTP 502' }]); - assert.ok(err.includes('role="alert"') && err.includes('Could not preview o/r#4 (HTTP 502)') && err.includes('ham-plan-retry">Retry'), err); + // The inline error names the hive, says why in its words, and says the + // decision stays off without a preview (hivecommons/hive#16776). An + // unexplained HTTP failure offers Retry. + const err = stageTrackerDelegatePlanErrorContent('o/r#4', [{ ref: 'o/r#4', error: 'HTTP 500', status: 500, reason: 'http' }], 'hive.example'); + assert.ok(err.includes('role="alert"') && err.includes('Could not preview o/r#4: the hive at hive.example answered HTTP 500 — HTTP 500.') && err.includes('ham-plan-retry">Retry'), err); + assert.ok(err.includes(STAGE_TRACKER_PLAN_NO_PREVIEW_NO_DECISION), err); + // The hosted gateway's generic 502 page is named as such, not as the hive's answer. + const gw = stageTrackerDelegatePlanFailure({ repo: 'o/r', kind: 'pr', number: 4 }, 502, { ok: false, error: 'hive unavailable', detail: 'restarting' }); + assert.strictEqual(gw.reason, 'gateway'); assert.strictEqual(gw.retryable, true); + const gwLines = stageTrackerDelegatePlanFailureLines(gw, 'the hive at hive.example'); + assert.ok(gwLines[0].includes('did not answer (HTTP 502 from the gateway') && gwLines[1].includes('Retry in a few seconds'), gwLines); + // The server's structured failures: a merged PR is final (no Retry); + // a GitHub failure after the server's own retries says how many and offers Retry. + const closed = stageTrackerDelegatePlanFailure({ repo: 'o/r', kind: 'pr', number: 4 }, 409, { ok: false, error: 'o/r#4 is not open', reason: 'not-open', retryable: false, hint: 'o/r#4 is closed on GitHub — refresh.', hive: 'Hive Commons' }); + const closedHtml = stageTrackerDelegatePlanErrorContent('o/r#4', [closed], 'hive.example'); + assert.ok(closedHtml.includes('Hive Commons reports it is no longer open.') && closedHtml.includes('o/r#4 is closed on GitHub — refresh.') && !closedHtml.includes('ham-plan-retry'), closedHtml); + const ghf = stageTrackerDelegatePlanFailure({ repo: 'o/r', kind: 'pr', number: 4 }, 424, { ok: false, error: 'reading checks: 500', reason: 'github', retryable: true, attempts: 3, elapsed_ms: 12400, hint: 'GitHub failed; Retry.', hive: 'Hive Commons' }); + const ghLines = stageTrackerDelegatePlanFailureLines(ghf, 'Hive Commons'); + assert.strictEqual(ghLines[0], 'Could not preview o/r#4: Hive Commons could not read it from GitHub after 3 attempts over 12s.'); + assert.strictEqual(ghLines[1], 'GitHub failed; Retry. (reading checks: 500)'); + assert.ok(stageTrackerDelegatePlanErrorContent('o/r#4', [ghf]).includes('ham-plan-retry')); + assert.strictEqual(stageTrackerDelegatePlanRetryable([closed]), false); + assert.strictEqual(stageTrackerDelegatePlanRetryable([closed, ghf]), true); + assert.strictEqual(stageTrackerDelegatePlanFailure({ repo: 'o/r', kind: 'pr', number: 4 }, 0, { error: 'Load failed', reason: 'network', retryable: true }).reason, 'network'); assert.strictEqual(stageTrackerDelegatePlanSubject([item, item]), '2 items'); const plan = { would: 'approve', summary: 'Approve o/r#4.', why: 'The question has one sane answer.', consequences: ['o/r#4 moves on'], alternatives: [] }; @@ -293,10 +315,10 @@ func TestStageTrackerDelegatePreviewSkeletonPrefetch(t *testing.T) { // A failed plan reads inline (no toast) and runs nothing. dialogs.length = 0; - reply = () => ({ ok: false, status: 502, json: async () => ({ error: 'GitHub timed out' }) }); + reply = () => ({ ok: false, status: 424, json: async () => ({ error: 'GitHub timed out', reason: 'github', retryable: true, hint: 'Retry.', hive: 'Hive Commons' }) }); assert.strictEqual(await stageTrackerActionPayload([item], 'decide:delegate'), null); assert.deepStrictEqual(dialogs.map(d => d.kind), ['plan-error']); - assert.ok(dialogs[0].cfg.html.includes('Could not preview o/r#4 (GitHub timed out)') && dialogs[0].cfg.html.includes('Retry'), dialogs[0].cfg.html); + assert.ok(dialogs[0].cfg.html.includes('Could not preview o/r#4: Hive Commons could not read it from GitHub.') && dialogs[0].cfg.html.includes('Retry'), dialogs[0].cfg.html); // Only an enabled Let hive decide (or one in a submenu) prefetches from the menu. assert.strictEqual(stageTrackerMenuOffersDelegate([{ id: 'decide:approve' }, { id: 'more', submenu: [{ id: 'decide:delegate' }] }]), true); diff --git a/src/pkg/dashboard/stage_tracker_row_actions_test.go b/src/pkg/dashboard/stage_tracker_row_actions_test.go index c17fda5823..8eaeafbd56 100644 --- a/src/pkg/dashboard/stage_tracker_row_actions_test.go +++ b/src/pkg/dashboard/stage_tracker_row_actions_test.go @@ -888,6 +888,8 @@ func stageTrackerConfirmOptOutJS(t *testing.T, html string) string { jsConstLine(t, html, "_stageTrackerPlanPrefetch"), jsFunc(t, html, "stageTrackerPlanRequestKey"), jsFunc(t, html, "stageTrackerPostDelegatePlan"), + jsConstLine(t, html, "STAGE_TRACKER_PLAN_GATEWAY_ERROR"), + jsFunc(t, html, "stageTrackerDelegatePlanFailure"), jsFunc(t, html, "stageTrackerPrefetchDelegatePlan"), jsFunc(t, html, "stageTrackerFetchDelegatePlan"), jsConstLine(t, html, "STAGE_TRACKER_PLAN_SKELETON_LINES"), @@ -895,6 +897,10 @@ func stageTrackerConfirmOptOutJS(t *testing.T, html string) string { jsConstLine(t, html, "STAGE_TRACKER_PLAN_BUTTONS_OPEN"), jsFunc(t, html, "stageTrackerDelegatePlanSkeletonContent"), jsFunc(t, html, "stageTrackerDelegatePlanSkeletonHtml"), + jsConstLine(t, html, "STAGE_TRACKER_PLAN_NO_PREVIEW_NO_DECISION"), + jsFunc(t, html, "stageTrackerDelegatePlanHiveLabel"), + jsFunc(t, html, "stageTrackerDelegatePlanFailureLines"), + jsFunc(t, html, "stageTrackerDelegatePlanRetryable"), jsFunc(t, html, "stageTrackerDelegatePlanErrorContent"), jsFunc(t, html, "stageTrackerDelegatePlanSubject"), jsConstLine(t, html, "_stageTrackerDelegatePreviewing"), diff --git a/src/pkg/dashboard/static-src/css/tracker.css b/src/pkg/dashboard/static-src/css/tracker.css index 014768ca26..65c4ac18b3 100644 --- a/src/pkg/dashboard/static-src/css/tracker.css +++ b/src/pkg/dashboard/static-src/css/tracker.css @@ -561,6 +561,8 @@ @keyframes ham-plan-skeleton-pulse { 50% { opacity: .55; } } @media (prefers-reduced-motion: reduce) { .ham-plan-skeleton-line { animation: none; } } .ham-plan-error { margin: 0 0 var(--sp-4); color: var(--status-error); } + .ham-plan-error-why { margin: 0 0 var(--sp-4); color: var(--muted); } + .ham-plan-error-rails { margin: 0 0 var(--sp-4); font-size: var(--fs-xs); color: var(--text-faint); } .ham-plan-merge[hidden] { display: none; } /* A covered issue's "decision pending on #N" chip goes to the PR's row. */ .ham-decision-pending { font-family: inherit; font-size: var(--fs-xs); background: transparent; cursor: pointer; } diff --git a/src/pkg/dashboard/static-src/js/tracker-actions.js b/src/pkg/dashboard/static-src/js/tracker-actions.js index 2b0d586576..997c517397 100644 --- a/src/pkg/dashboard/static-src/js/tracker-actions.js +++ b/src/pkg/dashboard/static-src/js/tracker-actions.js @@ -1762,9 +1762,30 @@ if (prefetch) headers['X-Hive-Prefetch'] = '1'; const r = await fetch(req.url + '/plan', { method: 'POST', headers, body: JSON.stringify(req.body) }); const data = await r.json().catch(() => ({})); - if (!r.ok) return { ref: stageTrackerRefOf(item), number: item.number, error: data.error || ('HTTP ' + r.status) }; + if (!r.ok) return stageTrackerDelegatePlanFailure(item, r.status, data); return Object.assign({ ref: stageTrackerRefOf(item) + (typeof stageTrackerLineageClause === 'function' ? stageTrackerLineageClause(item) : '') }, data); - } catch (e) { return { ref: stageTrackerRefOf(item), number: item.number, error: String(e && e.message || e) }; } + } catch (e) { return stageTrackerDelegatePlanFailure(item, 0, { error: String(e && e.message || e), reason: 'network', retryable: true }); } + } + // Pure: one failed plan — the server's structured failure (reason, + // hint, retryable, hive, attempts, elapsed_ms; delegatedPlanFailure in + // api_repo_item_decide_delegated_plan.go) when it answered, else what + // the transport said. A 502/503 whose body is the hosted gateway's + // generic "hive unavailable" page never came from the hive at all. + const STAGE_TRACKER_PLAN_GATEWAY_ERROR = 'hive unavailable'; + function stageTrackerDelegatePlanFailure(item, status, data) { + data = data || {}; + const gateway = status >= 502 && status <= 504 && (!data.reason || data.error === STAGE_TRACKER_PLAN_GATEWAY_ERROR); + return { + ref: stageTrackerRefOf(item), number: item.number, + error: data.error || ('HTTP ' + status), + status, + reason: gateway ? 'gateway' : (data.reason || (status ? 'http' : 'network')), + retryable: gateway || data.retryable === true || (!data.reason && status === 0), + hint: data.hint || '', + hive: data.hive || '', + attempts: data.attempts || 0, + elapsed_ms: data.elapsed_ms || 0, + }; } // Fire-and-forget: one plan request per Let hive decide target of the // row, at most once per STAGE_TRACKER_PLAN_PREFETCH_MS. Returns how many @@ -1829,15 +1850,62 @@ + '' + ''; } - // Pure: the inline error that replaces the skeleton, with Retry. - function stageTrackerDelegatePlanErrorContent(subject, failed) { + // Pure: the inline error that replaces the skeleton, with Retry. The + // old one-liner — "Could not preview o/r#N (hive unavailable)" — told + // the owner nothing (hivecommons/hive#16776: that message was a merged + // PR once and a GitHub 500 the next time). Each failure now names the + // hive that answered, says why in its own words, and the last line + // says plainly that Let hive do that stays off until a preview loads — + // the hive never records a decision it could not preview. + const STAGE_TRACKER_PLAN_NO_PREVIEW_NO_DECISION = 'Let hive do that stays off until a preview loads — the hive never decides what it could not preview.'; + // Pure: which hive the failure came from, for the message — the + // server's own name when it answered, else the host the page is on. + function stageTrackerDelegatePlanHiveLabel(failed, host) { + for (const pl of failed || []) if (pl && pl.hive) return String(pl.hive); + host = host || (typeof location !== 'undefined' && location.host) || ''; + return host ? 'the hive at ' + host : 'the hive'; + } + // Pure: the explanation lines for one failed plan: [what, why/do]. + function stageTrackerDelegatePlanFailureLines(pl, hive) { + pl = pl || {}; + const ref = pl.ref || ('#' + pl.number); + const err = String(pl.error || 'no plan'); + const tries = pl.attempts > 1 ? ' after ' + pl.attempts + ' attempts' + (pl.elapsed_ms ? ' over ' + Math.round(pl.elapsed_ms / 1000) + 's' : '') : ''; + switch (pl.reason) { + case 'gateway': + return ['Could not preview ' + ref + ': ' + hive + ' did not answer (HTTP ' + pl.status + ' from the gateway in front of it).', 'The hive is restarting or its API was briefly unreachable; the gateway replaced its reply with a generic page. Retry in a few seconds.']; + case 'not-open': + return ['Could not preview ' + ref + ': ' + hive + ' reports it is no longer open.', pl.hint || (ref + ' was merged or closed after this row loaded, so there is nothing left to decide. Refresh the activity monitor.')]; + case 'github': + return ['Could not preview ' + ref + ': ' + hive + ' could not read it from GitHub' + tries + '.', (pl.hint || err) + (pl.hint ? ' (' + err + ')' : '')]; + case 'config': + return ['Could not preview ' + ref + ': ' + hive + ' has no GitHub client configured.', pl.hint || err]; + case 'network': + return ['Could not preview ' + ref + ': could not reach ' + hive + ' (' + err + ').', 'Check your connection, then Retry.']; + default: + return ['Could not preview ' + ref + ': ' + hive + ' answered' + (pl.status ? ' HTTP ' + pl.status : '') + tries + ' — ' + err + '.', pl.hint || 'Retry once; report it if it persists.']; + } + } + // Pure: does Retry stand a chance — any failure the server or the + // transport marked retryable, or one with no word either way. + function stageTrackerDelegatePlanRetryable(failed) { + return (failed || []).some(pl => !pl || (pl.retryable !== false && pl.reason !== 'not-open' && pl.reason !== 'config')); + } + function stageTrackerDelegatePlanErrorContent(subject, failed, host) { const esc = hiveDialogEscape; - const what = (failed || []).map(pl => (pl.ref || ('#' + pl.number)) + ' (' + pl.error + ')').join(', '); + failed = (failed || []).map(pl => pl || { error: 'no plan' }); + const hive = stageTrackerDelegatePlanHiveLabel(failed, host); + const blocks = failed.map(pl => { + const lines = stageTrackerDelegatePlanFailureLines(pl, hive); + return '' + (lines[1] ? '

' + esc(lines[1]) + '

' : ''); + }).join(''); + const retry = stageTrackerDelegatePlanRetryable(failed); return '
' + esc(STAGE_TRACKER_PLAN_TITLE) + '
' + '
' + esc(subject) + '
' + '
' - + '' - + '' + + blocks + + '

' + esc(STAGE_TRACKER_PLAN_NO_PREVIEW_NO_DECISION) + '

' + + (retry ? '' : '') + '
'; } // Pure: the subject line the skeleton shows before the plan names it. @@ -1886,7 +1954,9 @@ const failed = (plans || []).filter(pl => !pl || pl.error); if (failed.length || !plans || !plans.length) { content.innerHTML = stageTrackerDelegatePlanErrorContent(opts.subject || '', failed.length ? failed : [{ ref: opts.subject || '', error: 'no plan' }]); - const retry = content.querySelector('.ham-plan-retry'); + // Retry takes focus when it is offered; else Cancel, the one + // button left that does anything. + const retry = content.querySelector('.ham-plan-retry') || overlay.querySelector('.cancel'); if (retry) retry.focus(); return; } diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 625f2102b9..099dc5a9b3 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1901,6 +1901,8 @@ @keyframes ham-plan-skeleton-pulse { 50% { opacity: .55; } } @media (prefers-reduced-motion: reduce) { .ham-plan-skeleton-line { animation: none; } } .ham-plan-error { margin: 0 0 var(--sp-4); color: var(--status-error); } + .ham-plan-error-why { margin: 0 0 var(--sp-4); color: var(--muted); } + .ham-plan-error-rails { margin: 0 0 var(--sp-4); font-size: var(--fs-xs); color: var(--text-faint); } .ham-plan-merge[hidden] { display: none; } /* A covered issue's "decision pending on #N" chip goes to the PR's row. */ .ham-decision-pending { font-family: inherit; font-size: var(--fs-xs); background: transparent; cursor: pointer; } @@ -24113,9 +24115,30 @@

All labels

if (prefetch) headers['X-Hive-Prefetch'] = '1'; const r = await fetch(req.url + '/plan', { method: 'POST', headers, body: JSON.stringify(req.body) }); const data = await r.json().catch(() => ({})); - if (!r.ok) return { ref: stageTrackerRefOf(item), number: item.number, error: data.error || ('HTTP ' + r.status) }; + if (!r.ok) return stageTrackerDelegatePlanFailure(item, r.status, data); return Object.assign({ ref: stageTrackerRefOf(item) + (typeof stageTrackerLineageClause === 'function' ? stageTrackerLineageClause(item) : '') }, data); - } catch (e) { return { ref: stageTrackerRefOf(item), number: item.number, error: String(e && e.message || e) }; } + } catch (e) { return stageTrackerDelegatePlanFailure(item, 0, { error: String(e && e.message || e), reason: 'network', retryable: true }); } + } + // Pure: one failed plan — the server's structured failure (reason, + // hint, retryable, hive, attempts, elapsed_ms; delegatedPlanFailure in + // api_repo_item_decide_delegated_plan.go) when it answered, else what + // the transport said. A 502/503 whose body is the hosted gateway's + // generic "hive unavailable" page never came from the hive at all. + const STAGE_TRACKER_PLAN_GATEWAY_ERROR = 'hive unavailable'; + function stageTrackerDelegatePlanFailure(item, status, data) { + data = data || {}; + const gateway = status >= 502 && status <= 504 && (!data.reason || data.error === STAGE_TRACKER_PLAN_GATEWAY_ERROR); + return { + ref: stageTrackerRefOf(item), number: item.number, + error: data.error || ('HTTP ' + status), + status, + reason: gateway ? 'gateway' : (data.reason || (status ? 'http' : 'network')), + retryable: gateway || data.retryable === true || (!data.reason && status === 0), + hint: data.hint || '', + hive: data.hive || '', + attempts: data.attempts || 0, + elapsed_ms: data.elapsed_ms || 0, + }; } // Fire-and-forget: one plan request per Let hive decide target of the // row, at most once per STAGE_TRACKER_PLAN_PREFETCH_MS. Returns how many @@ -24180,15 +24203,62 @@

All labels

+ '' + ''; } - // Pure: the inline error that replaces the skeleton, with Retry. - function stageTrackerDelegatePlanErrorContent(subject, failed) { + // Pure: the inline error that replaces the skeleton, with Retry. The + // old one-liner — "Could not preview o/r#N (hive unavailable)" — told + // the owner nothing (hivecommons/hive#16776: that message was a merged + // PR once and a GitHub 500 the next time). Each failure now names the + // hive that answered, says why in its own words, and the last line + // says plainly that Let hive do that stays off until a preview loads — + // the hive never records a decision it could not preview. + const STAGE_TRACKER_PLAN_NO_PREVIEW_NO_DECISION = 'Let hive do that stays off until a preview loads — the hive never decides what it could not preview.'; + // Pure: which hive the failure came from, for the message — the + // server's own name when it answered, else the host the page is on. + function stageTrackerDelegatePlanHiveLabel(failed, host) { + for (const pl of failed || []) if (pl && pl.hive) return String(pl.hive); + host = host || (typeof location !== 'undefined' && location.host) || ''; + return host ? 'the hive at ' + host : 'the hive'; + } + // Pure: the explanation lines for one failed plan: [what, why/do]. + function stageTrackerDelegatePlanFailureLines(pl, hive) { + pl = pl || {}; + const ref = pl.ref || ('#' + pl.number); + const err = String(pl.error || 'no plan'); + const tries = pl.attempts > 1 ? ' after ' + pl.attempts + ' attempts' + (pl.elapsed_ms ? ' over ' + Math.round(pl.elapsed_ms / 1000) + 's' : '') : ''; + switch (pl.reason) { + case 'gateway': + return ['Could not preview ' + ref + ': ' + hive + ' did not answer (HTTP ' + pl.status + ' from the gateway in front of it).', 'The hive is restarting or its API was briefly unreachable; the gateway replaced its reply with a generic page. Retry in a few seconds.']; + case 'not-open': + return ['Could not preview ' + ref + ': ' + hive + ' reports it is no longer open.', pl.hint || (ref + ' was merged or closed after this row loaded, so there is nothing left to decide. Refresh the activity monitor.')]; + case 'github': + return ['Could not preview ' + ref + ': ' + hive + ' could not read it from GitHub' + tries + '.', (pl.hint || err) + (pl.hint ? ' (' + err + ')' : '')]; + case 'config': + return ['Could not preview ' + ref + ': ' + hive + ' has no GitHub client configured.', pl.hint || err]; + case 'network': + return ['Could not preview ' + ref + ': could not reach ' + hive + ' (' + err + ').', 'Check your connection, then Retry.']; + default: + return ['Could not preview ' + ref + ': ' + hive + ' answered' + (pl.status ? ' HTTP ' + pl.status : '') + tries + ' — ' + err + '.', pl.hint || 'Retry once; report it if it persists.']; + } + } + // Pure: does Retry stand a chance — any failure the server or the + // transport marked retryable, or one with no word either way. + function stageTrackerDelegatePlanRetryable(failed) { + return (failed || []).some(pl => !pl || (pl.retryable !== false && pl.reason !== 'not-open' && pl.reason !== 'config')); + } + function stageTrackerDelegatePlanErrorContent(subject, failed, host) { const esc = hiveDialogEscape; - const what = (failed || []).map(pl => (pl.ref || ('#' + pl.number)) + ' (' + pl.error + ')').join(', '); + failed = (failed || []).map(pl => pl || { error: 'no plan' }); + const hive = stageTrackerDelegatePlanHiveLabel(failed, host); + const blocks = failed.map(pl => { + const lines = stageTrackerDelegatePlanFailureLines(pl, hive); + return '' + (lines[1] ? '

' + esc(lines[1]) + '

' : ''); + }).join(''); + const retry = stageTrackerDelegatePlanRetryable(failed); return '
' + esc(STAGE_TRACKER_PLAN_TITLE) + '
' + '
' + esc(subject) + '
' + '
' - + '' - + '' + + blocks + + '

' + esc(STAGE_TRACKER_PLAN_NO_PREVIEW_NO_DECISION) + '

' + + (retry ? '' : '') + '
'; } // Pure: the subject line the skeleton shows before the plan names it. @@ -24237,7 +24307,9 @@

All labels

const failed = (plans || []).filter(pl => !pl || pl.error); if (failed.length || !plans || !plans.length) { content.innerHTML = stageTrackerDelegatePlanErrorContent(opts.subject || '', failed.length ? failed : [{ ref: opts.subject || '', error: 'no plan' }]); - const retry = content.querySelector('.ham-plan-retry'); + // Retry takes focus when it is offered; else Cancel, the one + // button left that does anything. + const retry = content.querySelector('.ham-plan-retry') || overlay.querySelector('.cancel'); if (retry) retry.focus(); return; }