diff --git a/changelog.d/added-merge-queue-position.md b/changelog.d/added-merge-queue-position.md new file mode 100644 index 0000000000..5512436b71 --- /dev/null +++ b/changelog.d/added-merge-queue-position.md @@ -0,0 +1,2 @@ +- Activity Monitor: a merge-train member's chip reads its place in the queue — "In merge queue · 2 of 6 · ~4m" — with a countdown to its estimated auto-merge turn that survives poll re-renders, the same words whether or not a merge sweep is evaluating it (the sweep is the spinning glyph and a "Checking now…" hover line), a hover that names what the head waits on (soak left, CI, a maintainer) and the estimate's basis, a ⛓ Merge queue popover listing every train in merge order, and a stall warning ("Merge queue stalled · head #15253 waiting for CI 3h", one activity-log line per train per hour) past `governor.merge_train_stall_warn_s` (default 3600). `/api/stage-tracker` `merge_train` gains `length`, `head_number`, `head_state`, `head_reason`, `head_since`, `eta_s`, `eta_at`, `eta_basis`, `late`, `stalled`, `stalled_s` (#15395). +- Merge queue: one PR per issue — when two open PRs declare the same `Fixes #N`, the sweep keeps one (a green head first, then the earlier-opened) and holds the other out of every train with gate state `duplicate-of:` (chip "Duplicate of #15402 · needs a pick", `merge_gate.duplicate_of` / `duplicate_issue`, `needs-decision`); "Let hive decide" on either PR takes the existing supersede path (rule `one-pr-per-issue`: the held one closes as superseded with credit, the kept one is released) (#15395). diff --git a/src/docs/api-reference/auth.md b/src/docs/api-reference/auth.md index 6850135715..91913b0ffb 100644 --- a/src/docs/api-reference/auth.md +++ b/src/docs/api-reference/auth.md @@ -24,6 +24,6 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `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:1616` | -| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1617` | -| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1618` | +| `POST` | `/api/github-app/recheck` | Dashboard auth/session | GitHub App Recheck | `pkg/dashboard/server.go:1621` | +| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1622` | +| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1623` | diff --git a/src/docs/api-reference/dashboard-misc.md b/src/docs/api-reference/dashboard-misc.md index fbfc46e390..90add518a2 100644 --- a/src/docs/api-reference/dashboard-misc.md +++ b/src/docs/api-reference/dashboard-misc.md @@ -39,7 +39,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `GET` | `/api/budget-ignore` | Dashboard auth/session | Budget Ignore Get | `pkg/dashboard/api.go:172` | | `POST` | `/api/budget-ignore` | Owner only | Budget Ignore Set | `pkg/dashboard/api.go:173` | | `GET` | `/api/summaries` | Dashboard auth/session | Summaries | `pkg/dashboard/api.go:185` | -| `GET` | `/api/stage-tracker` | Dashboard auth/session | Stage Tracker: every open issue and pull request of the watched repos for the Hive activity monitor — actionable, held, claimed, in-progress, covered-by-PR, filtered (`needs-*`), draft and exempt-labelled items alike, each with `stage`, `stage_since`, `labels`, `created_at`, `author_kind`, `modifiers`, `claimant: {kind: hive-agent|clanker|person, login, agent, hive, since, source: claim-comment|assignee|kick-history|author}` naming who holds a claimed / in-progress issue or a PR under review or rework (absent when nobody is named; mirrored onto `linked_prs[]`), (PRs) `sentinel_alert` / `sentinel_findings` / `sentinel_reason` when the sentinel flagged the PR, `decision_needed` on a row parked on a person (085 / 045 / 015, the needs-human pill; mirrored onto the issue's `linked_prs[]` entry): `{question, asked_by, since, blocker, blocker_reason, options: [{action, label, enabled, reason_if_disabled}]}` — the reviewer's own decision question when the verdict record has one, the blocker in plain words, and the applicable actions under the row context menu's action ids (`decide:delegate` first), (PRs) `lineage` on the hive's copy of an adopted PR — `{kind: adopted|superseded|rebased-by-hive, from_number, from_url, from_author, from_author_kind, to_number, to_url, at, reason}`, derived from the stage store's adoption record or the `hive/adopt-` head branch, mirrored onto the issue's `linked_prs[]` entry; absent on every other row — and (issues) `linked_prs`; items with no stage yet report stage `0` "Open"; `needs_human_labels` is the "a person must act" label set behind the monitor's Needs human facet (the same list as `/api/status.labelPresets[needs-human]`); `recently_merged` lists the PRs (`repo`, `number`, `merged_at`) whose `pr_merged` audit entry is under 30 minutes old, so a client diffing two payloads can tell a merge from a close. Served from a server-side cache rebuilt at most every `dashboard.stage_tracker.cache_ttl_s` (default `governor.eval_interval_s`) from the spoke's existing snapshot — a request never calls GitHub. Carries `generated_at`, `source` (`cache` or `fresh`), `cache_ttl_s`, an `ETag` and `Cache-Control: private, max-age=`; `If-None-Match` yields 304. Each hit is recorded under REST caller `dashboard:stage-tracker` so `/api/gh-rate-limits` shows it charged nothing. | `pkg/dashboard/api.go:186` | +| `GET` | `/api/stage-tracker` | Dashboard auth/session | Stage Tracker: every open issue and pull request of the watched repos for the Hive activity monitor — actionable, held, claimed, in-progress, covered-by-PR, filtered (`needs-*`), draft and exempt-labelled items alike, each with `stage`, `stage_since`, `labels`, `created_at`, `author_kind`, `modifiers`, `claimant: {kind: hive-agent|clanker|person, login, agent, hive, since, source: claim-comment|assignee|kick-history|author}` naming who holds a claimed / in-progress issue or a PR under review or rework (absent when nobody is named; mirrored onto `linked_prs[]`), (PRs) `sentinel_alert` / `sentinel_findings` / `sentinel_reason` when the sentinel flagged the PR, `decision_needed` on a row parked on a person (085 / 045 / 015, the needs-human pill; mirrored onto the issue's `linked_prs[]` entry): `{question, asked_by, since, blocker, blocker_reason, options: [{action, label, enabled, reason_if_disabled}]}` — the reviewer's own decision question when the verdict record has one, the blocker in plain words, and the applicable actions under the row context menu's action ids (`decide:delegate` first), (PRs) `lineage` on the hive's copy of an adopted PR — `{kind: adopted|superseded|rebased-by-hive, from_number, from_url, from_author, from_author_kind, to_number, to_url, at, reason}`, derived from the stage store's adoption record or the `hive/adopt-` head branch, mirrored onto the issue's `linked_prs[]` entry; absent on every other row — and (issues) `linked_prs`; items with no stage yet report stage `0` "Open"; `needs_human_labels` is the "a person must act" label set behind the monitor's Needs human facet (the same list as `/api/status.labelPresets[needs-human]`); `recently_merged` lists the PRs (`repo`, `number`, `merged_at`) whose `pr_merged` audit entry is under 30 minutes old, so a client diffing two payloads can tell a merge from a close; a PR in a conflict-aware merge train carries `merge_train` `{id, state, front, position, size, attempt, max_attempts, overlap_files, ahead, length, head_number, head_state, head_reason, head_since, eta_s, eta_at, eta_basis, late, stalled, stalled_s}` — `head_state` one of `needs-human` / `ci` / `soaking` / `ready` / `merging` / `fixing` / `unknown` with `head_reason` in plain words; `eta_s` / `eta_at` the estimated wait for this member's own merge attempt (own remaining soak + each PR ahead's remaining soak or one sweep + the next sweep tick; absent while the head waits on a person or CI), `eta_at` anchored to the member's first estimate at its position so a countdown survives polls, `eta_basis` its short reading ("2 ahead · head soaking 12m · next sweep in 40s"), `late` once now has overrun the first estimate twice over, `stalled` / `stalled_s` once the head has sat longer than `governor.merge_train_stall_warn_s` ([#15395](https://github.com/hivecommons/hive/issues/15395)); a PR the queue holds because another open PR declares the same `Fixes #N` carries no `merge_train` and a `merge_gate` of `{state: "duplicate-of:", duplicate_of, duplicate_issue}` plus the `needs-decision` modifier (the queue keeps one PR per issue: green first, then the earlier-opened). Served from a server-side cache rebuilt at most every `dashboard.stage_tracker.cache_ttl_s` (default `governor.eval_interval_s`) from the spoke's existing snapshot — a request never calls GitHub. Carries `generated_at`, `source` (`cache` or `fresh`), `cache_ttl_s`, an `ETag` and `Cache-Control: private, max-age=`; `If-None-Match` yields 304. Each hit is recorded under REST caller `dashboard:stage-tracker` so `/api/gh-rate-limits` shows it charged nothing. | `pkg/dashboard/api.go:186` | | `POST` | `/api/prs/{owner}/{repo}/{number}/queue-automerge` | Merger/owner role | Queue PRAuto Merge | `pkg/dashboard/api.go:187` | | `GET` | `/api/inference/models/{backend}` | Dashboard auth/session | Inference Models | `pkg/dashboard/api.go:363` | | `GET` | `/api/hive-id` | Dashboard auth/session | Hive IDGet | `pkg/dashboard/api.go:425` | @@ -63,4 +63,4 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `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:1623` | +| `GET` | `/sso` | Public | SSO | `pkg/dashboard/server.go:1628` | diff --git a/src/docs/api-reference/health-status-events.md b/src/docs/api-reference/health-status-events.md index 415c13cd6b..8b8043df8c 100644 --- a/src/docs/api-reference/health-status-events.md +++ b/src/docs/api-reference/health-status-events.md @@ -8,17 +8,17 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `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:43` | | `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:44` | | `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:65` | -| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1596` | -| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1597` | -| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1598` | -| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1604` | -| `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); `ci_failure_signals` `{count, last, kicks, unmatched}` is the webhook CI-failure signal store — distinct red (repo, head, check) signals retained, the most recent one `{repo, number, head_sha, check, conclusion, details_url, agent, at}`, webhook-triggered kicks/nudges since boot, and failure deliveries whose head matched no open PR; `hotspots` `{count, actionable, filed}` is the merge-hotspot advisor's one-line summary (files at/above the thresholds, those with a split recommendation, those with a modularize issue filed), absent when the advisor is not wired — details on `GET /api/hotspots` | `pkg/dashboard/server.go:1609` | -| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1610` | -| `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:1611` | -| `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:1612` | -| `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:1613` | -| `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:1614` | -| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1615` | +| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1601` | +| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1602` | +| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1603` | +| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1609` | +| `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); `ci_failure_signals` `{count, last, kicks, unmatched}` is the webhook CI-failure signal store — distinct red (repo, head, check) signals retained, the most recent one `{repo, number, head_sha, check, conclusion, details_url, agent, at}`, webhook-triggered kicks/nudges since boot, and failure deliveries whose head matched no open PR; `hotspots` `{count, actionable, filed}` is the merge-hotspot advisor's one-line summary (files at/above the thresholds, those with a split recommendation, those with a modularize issue filed), absent when the advisor is not wired — details on `GET /api/hotspots` | `pkg/dashboard/server.go:1614` | +| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1615` | +| `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:1616` | +| `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:1617` | +| `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:1618` | +| `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:1619` | +| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1620` | | `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` | diff --git a/src/docs/api-reference/snapshots-and-style.md b/src/docs/api-reference/snapshots-and-style.md index 2530d0bbcf..6d6cf200ef 100644 --- a/src/docs/api-reference/snapshots-and-style.md +++ b/src/docs/api-reference/snapshots-and-style.md @@ -6,7 +6,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | Method | Path | Auth | Purpose | Source | |---|---|---|---|---| | `GET` | `/api/style` | Public | Sanitized custom dashboard CSS | `pkg/dashboard/api.go:45` | -| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](../branding.md)) | `pkg/dashboard/server.go:1676` | +| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](../branding.md)) | `pkg/dashboard/server.go:1681` | | `GET` | `/api/snapshot/frame-ancestors` | Public | Snapshot framing allowlist | `pkg/dashboard/api.go:78` | | `GET` | `/api/snapshot` | Public | Snapshot data | `pkg/dashboard/api.go:79` | | `GET` | `/snapshot` | Public | Public read-only snapshot page | `pkg/dashboard/api.go:80` | diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 83392b9582..f93f5102f8 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -909,10 +909,18 @@ It counts under **Needs attention**. Before the merge gate only the held-workflo #### Merge train -When several approved PRs to the same base touch the same files, the automerge lanes merge them one at a time ([#15117](https://github.com/hivecommons/hive/issues/15117)). The tracker renders every train contiguously, in merge order, with a thin left rail linking the rows and a header chip `⛓ merge train · 4 PRs · front #15074` (overlapping files listed beside it). Pill wording per state: the front reads "Merging · front of train"; siblings "In merge train · 3rd of 4"; once the front merged and the hive is rebasing a conflicting sibling, "Rebasing · attempt 1/2"; when the loop guard (`auto_merge.merge_train.max_rebase_attempts`) is spent, "Needs human decision · rebase failed twice" at stage/085 with the decision option "Rebase, then merge as maintainer". +When several approved PRs to the same base touch the same files, the automerge lanes merge them one at a time ([#15117](https://github.com/hivecommons/hive/issues/15117)). The tracker renders every train contiguously, in merge order, with a thin left rail linking the rows and a header chip `⛓ merge train · 4 PRs · front #15074` (overlapping files listed beside it). Pill wording per state: the front reads "Next to merge · 1 of 4 · ~13m" (its own soak left plus the next sweep); siblings "In merge queue · 2 of 4 · ~4m" (position, length, countdown to its turn); once the front merged and the hive is rebasing a conflicting sibling, "Rebasing · attempt 1/2"; when the loop guard (`auto_merge.merge_train.max_rebase_attempts`) is spent, "Needs human decision · rebase failed twice" at stage/085 with the decision option "Rebase, then merge as maintainer". The tooltip lists the PRs ahead and the overlapping files. Waiting siblings are never red, never `needs human`; a `needs-rebase` label on one is reported under `stale_labels`, the train repairs it. Each item carries `merge_gate.state` (`queued-behind` / `rebasing` / `rebase-exhausted` / `front`, with an honest `reason` such as `behind #15074 in merge train (3rd of 4, overlapping files: stage_tracker.go, index.html)`) and `merge_train{id, state, front, position, size, attempt, max_attempts, overlap_files, ahead}`; the id is `#`. +**Queue position, countdown, stall** ([#15395](https://github.com/hivecommons/hive/issues/15395)). The queued chip is its place in the queue and nothing else — **In merge queue · 2 of 6 · ~4m** — so it reads the same whether or not a merge sweep is evaluating it: the sweep is the spinning ⬡ glyph and a *Checking now…* line in the hover, never a change of words, and no state slug (`queued-behind`) ever reaches the page. The hover names what the row waits on — *Behind #15253 (soaking, 12m left) and 1 more. Hive merges one at a time, in order. Estimated turn: ~25m (2 ahead · head soaking 12m · next sweep in 40s).* — or, when the head is on a person or on CI, the blocker instead of a clock: *Head #15253 is waiting for a maintainer (untrusted author); no estimate until it moves.* The server adds to each member's `merge_train`: `length`, `head_number`, `head_state` (`needs-human` / `ci` / `soaking` / `ready` / `merging` / `fixing` / `unknown`), `head_reason` (plain words), `head_since`, `eta_s`, `eta_at`, `eta_basis`, `late`, `stalled`, `stalled_s`. + +**One PR per issue** ([#15395](https://github.com/hivecommons/hive/issues/15395)). Two open PRs declaring the same `Fixes #N` never both ride the queue (hive's own #15402 and #15407, both for #15396, once sat side by side as "waiting its turn"). Before the trains are grouped the sweep keeps one PR per closing issue — a green head (not conflicting, not blocked on a red required check) beats a red one; among equals the earlier-opened wins — and holds the other out of every train with the stage reason `merge-train:duplicate-of#:/0:0/0:`. The held row's gate reads `duplicate-of:` (`merge_gate.duplicate_of`, `merge_gate.duplicate_issue`), its chip **Duplicate of #15402 · needs a pick** with no clock, it carries no `merge_train`, and it wears `needs-decision` so both PRs sit under their issue with the hold visible. *Let hive decide* on either row takes the existing supersede path (rule `one-pr-per-issue`): on the held PR it closes it as superseded by the kept one with a credit comment, through the same write gate as every delegate close; on the kept PR it releases it and names the duplicate the queue holds (or, if the kept PR has since gone red, only recommends the fix — the queue re-picks on its next pass, so an earlier red PR yields to a later green one). + +- **Countdown to auto-merge.** `eta_s` is the member's own remaining soak, plus — for each PR ahead — its remaining soak or, with no clock of its own, one sweep (the lanes' floor cadence), plus the wait for the next sweep tick; `eta_basis` is the short reading ("2 ahead · head soaking 12m · next sweep in 40s"). The chip's `· ~4m` counts down in place from `eta_at` on the same tick as the soak clock, and `eta_at` is anchored server-side to the member's first estimate at its position (re-anchored only when a fresh estimate runs more than a sweep longer), so a poll re-render never resets it. Past the estimate it reads *any sweep now*; past twice the first estimate `late` is set and the chip reads *running late* with the estimate kept in the hover. Always a `~`: an estimate, not a promise. A head on a person, on CI or being fixed — or a member that is itself a person's — carries no estimate and the blocker instead. +- **Merge queue popover.** The sticky bar's **⛓ Merge queue N** chip (hidden with no train) opens a popover listing every train in merge order — ordinal, #, title, state in words, ETA — with the head's blocker on the front row; same pattern as the **Merged** popover (click outside / Escape closes). +- **Stall warning.** Once the head has sat in one stage longer than `governor.merge_train_stall_warn_s` (default 3600) and is not merging — a soaking head counts only once its own clock has overrun — every member's chip turns to **Merge queue stalled · head #15253 waiting for CI 3h**, the popover marks the train, and the activity log records one `merge train stalled` line per train per hour. + #### Pills before the merge gate Before the merge gate, the tracker defers general `sentinel`, `needs human` and `unauthorized` pills so they do not distract from work still in motion. **Exception:** `unauthorized:approve-workflow-runs` appears immediately, because a person must approve the held run before CI can start. @@ -1173,6 +1181,7 @@ Every PR at the gate now carries `merge_gate: {state, reason, sweep_running, las | 080 awaiting merge admission | **Ready for auto merge** | *next sweep in 42s* (counts down in place; *sweep due now* once past) | 🕓 clock — *Approved and green; waiting for hive's merge sweep — next sweep in 42s. …* | | 095 pre-merge soak | **Soaking** | *7m left* (counts down; *soak window complete* once past) | 🕓 clock — *Admitted by the automerge lane; waiting for the soak window: 7m left. hive merges on its first sweep after that.* | | any of the above while a pass is evaluating this PR | the stage's word | *merge sweep running* | ⟳ (static) — *hive's merge sweep is running on this PR now.* | +| 080 in a merge train (`merge_gate.state: queued-behind` / `front`) | **In merge queue · 2 of 6** (position of length; **Merge queue stalled · head #15253 waiting for CI 3h** once stalled) | *~4m* (counts down to its estimated turn from `merge_train.eta_at`; *any sweep now* once past; *running late* past twice the first estimate; nothing while the head is on a person or CI) — the words never change with the sweep | 🕓 clock, or ⟳ spinning while a sweep evaluates it — *Behind #15253 (soaking, 12m left) and 1 more. Hive merges one at a time, in order. Estimated turn: ~25m (2 ahead · head soaking 12m · next sweep in 40s). Checking now… Last checked 12s ago.* | | 100 merge queue | **Merging** | — | ⟳ (static) — *Admitted and soaked; hive is merging it now (GitHub merge queue / merge window).* | | any stage while hive's merge call is in flight (`merge_gate.merging_since`, `merge_path`) | **Merging** | *merge call in flight*, then *merge landed* while the SPA holds the row | ⟳ (static) — *hive is merging it now (automerge sweep) — the merge call has been in flight since 3s ago.* | | any stage after GitHub refused hive's merge call (`merge_gate.state: merge-failed`, `merge_failed`, `merge_failed_kind`, `merge_failed_text`, `merge_failed_at`) | **Merge failed** | the refusal translated to a short phrase — *branch protection*, *needs rebase*, *not allowed*, *review required*, or *retrying* while the sweep is on it; never GitHub's raw text | 🕓 clock — *hive tried to merge this (maintainer merge from the dashboard) 9s ago and GitHub refused — branch protection blocks it. The lane re-evaluates it on its next pass. Details: 405 Pull Request is not mergeable* | @@ -1305,7 +1314,7 @@ The rules live in one place — `src/pkg/delegatedecision/policy.go` (`PolicyTex | a hive-filed proposal **without** such evidence | `proposal-not-now` (verdict `not-now`) | Declined for now: a comment saying "no incident or recurrence justifies the cost yet; re-propose with evidence", then closed as not planned (the established declined-proposal path). Never applied to a person's proposal (those keep `default-approve`), never to the hive's own policy (`hive-policy` stays recommend-only). | | a change to the hive's own policy (for example an RFC to hold unacknowledged hive-filed proposals) | `hive-policy` (recommend-only) | Unchanged in shape, but the recommendation is a real sentence in the owner's terms — "approve `` as proposed — proposals nobody has acknowledged should not be actionable; make it default-on for the lanes it names rather than opt-in; owner to confirm" — never "no special rule applies". | -Each decision names the rule that fired (`dedupe`, `owner-only`, `flaky-test`, `ci-ratchet`, `design-proposal`, `review-backlog`, `selfreport-operator-action`, `proposal-approve-evidence`, `proposal-not-now`, `default-approve`, `prefer-contributor`, `contributor-offline`, `sentinel-trusted`, `small-low-risk`, `reporter-trust`, `fix-then-merge`, `default-release`, `hive-policy`, `sentinel-untrusted`, `rail-hold`) in the comment and in the job's `decision.rule`. +Each decision names the rule that fired (`dedupe`, `owner-only`, `flaky-test`, `ci-ratchet`, `design-proposal`, `review-backlog`, `selfreport-operator-action`, `proposal-approve-evidence`, `proposal-not-now`, `default-approve`, `prefer-contributor`, `contributor-offline`, `sentinel-trusted`, `small-low-risk`, `reporter-trust`, `fix-then-merge`, `default-release`, `hive-policy`, `sentinel-untrusted`, `rail-hold`, `one-pr-per-issue`) in the comment and in the job's `decision.rule`. **Safety rails** (`ApplyRails`, applied after the policy and before anything is written): the delegate never merges a PR — it only records the approval the auto-merge engine already acts on per repository settings (a merger/owner requester's approval is written as the queue line the sweep parses; anyone else's as `/lgtm` + `/approve` comments attributed to them); it does not touch an item wearing `hold` (refused, nothing written — release the hold first if you want it decided); it never grants org membership, secrets or permissions (those items get `needs-owner` + `hold` and the one owner-action comment, and keep their park); and a question that is a policy or scope change to the hive itself (settings, governance docs, security posture — the `governance` label or those words in the park reason) is left parked: the delegate posts "hive recommends: …; owner to confirm" and keeps 085. diff --git a/src/docs/labels-and-control-signals.md b/src/docs/labels-and-control-signals.md index c1226ac27a..f41013123b 100644 --- a/src/docs/labels-and-control-signals.md +++ b/src/docs/labels-and-control-signals.md @@ -2371,6 +2371,7 @@ The plain-words comment the delegate posts has four lines — **Hive decided** ( | `hive-policy` | this changes the hive's own rules | This changes the hive's own settings, governance or security posture; the hive only recommends and the owner confirms. | | `llm-delegate` | decided by the hive's model under the owner's rules | No mechanical rule applied, so the hive's model decided under the owner's standing rules; the same safety rails applied. | | `mergeable-as-is` | mergeable and green as it is; handed to the merge gate | The head is mergeable with green CI, so no adoption is needed: the hive approves it and the merge gate takes it as it is. | +| `one-pr-per-issue` | two PRs fix the same issue; the merge queue keeps one | Two open PRs declare the same closing issue. The merge queue carries one PR per issue — a green head first, then the earlier-opened — and the other closes as superseded with credit. | | `owner-only`, `owner_only` | only the hive owner can do this | This needs a secret, token or organization permission that only the hive owner holds; the hive cannot do it for you. | | `prefer-contributor` | a contributor's PR and a hive PR do the same work | A contributor's PR and a hive-authored PR cover the same work; the hive prefers the contributor's when it is mergeable and current. | | `rail-hold` | the item is on hold | The item carries the hold label; the hive never acts on a held item until a person releases the hold. | diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index f72be621ab..d4c7758a47 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -245,6 +245,7 @@ Go callers keep reading `cfg.Agents`, `cfg.Governor.Gateways` and so on through | `review.reviewers.autoscale.*` | `enabled: false`; when on: `min: 1`, `max: 4` (≤ the replicas cap 5), `target_backlog_per_reviewer: 2`, `scale_up_cycles: 2`, `scale_down_cycles: 5`, `cooldown: 5m`, `cpu_per_reviewer: 1.0`, `mem_per_reviewer_mb: 2048`, `agent` unset (first `role: reviewer` agent, else `reviewer_agents[0]`); owner-only. | Sizes the reviewer lane to its backlog ([#14685](https://github.com/hivecommons/hive/issues/14685)): adds a `reviewer-N` replica after `scale_up_cycles` cycles with more PRs waiting for a free reviewer than `target_backlog_per_reviewer` per reviewer (or a wait older than the review-pending stage dwell), drains the top replica after `scale_down_cycles` idle cycles and retires it only when its turn ends. Scale-ups are refused by named guards (`at-max`, `cooldown`, `saturated` kicks, `provider-budget`, `provider-headroom`, `max-parallel-reviews`, cgroup `cpu` / `memory`). Decisions log as `reviewer autoscale: 2→3 reason=…`; state is in `/api/status.reviewerAutoscale`. See [review-swarm.md](review-swarm.md#reviewer-autoscale-reviewreviewersautoscale). | | `review.contributor_prs.base_sync` | Off by default; owner-only. | Lets PR follow-up use GitHub's update-branch API on hive-authored lane PRs whose head is in a contributor fork, whose `mergeable_state` is `behind`/`dirty`, and whose author enabled "allow edits by maintainers". `behind` PRs get a GitHub-authored merge commit; `dirty`/422 falls back to the `hive-base-moved` repair note and kick. This requires an agent/proxy mode with `ISSUES_PRS_MERGE` because `PUT /pulls/{n}/update-branch` is merge-scoped. It does not rewrite history and does not add a human/bot-authored DCO commit; the repository's "Sign-off survives the squash" check examines the PR's own commits, not GitHub's update-branch merge commit. | | `governor.advisory.post_retry_backoff_s` | `3600` seconds. `HIVE_ADVISORY_POST_RETRY_BACKOFF_S` overrides the default when the key is unset; a negative value disables the pause. | How long the advisory digest write is paused after GitHub refuses it with `403 Resource not accessible by integration`. That refusal is structural (foreign-authored comment, repo dropped from the installation, or repo transferred to another owner), so per-cycle retries only burn API quota. The failed edit first falls back to creating a fresh App-authored comment; if the create is refused too, the error is recorded for the hub health row — naming the new `owner/repo` when the repository has moved — and attempts pause for this window. See [advisory.md](advisory.md#when-the-write-is-refused). | +| `governor.merge_train_stall_warn_s` | `3600` seconds; `0` disables | How long the head of a merge train may sit in one stage before the Activity Monitor reads the train **stalled** — the queued chip turns to "Merge queue stalled · head #15253 waiting for CI 3h", the Merge queue popover marks the train, and the activity log records one `merge train stalled` line per train per hour naming the head, its state and how long. A soaking head is on its clock, not stalled, until that clock itself overruns the window ([#15395](https://github.com/hivecommons/hive/issues/15395)). | | `governor.trajectory.enabled` | Defaults to enabled. | The lane no-ops until a reviewer endpoint and model resolve from `governor.trajectory` or `governor.litellm`. | | `governor.kick_limits.max_issues` / `max_prs` | `100` / `50` | Caps on every issue list and every PR list (actionable, stale drafts, merge-eligible, CI-failing) rendered into a kick prompt; a cut list ends with an explicit "… and N more" line. An absent key or a negative value means the default; an explicit `0` opts out of the cap entirely (`KickListUnlimited` — every item is listed), and anything above `500` is pinned to `500`. Editable without touching this file under **Settings → Repos → Kick prompt list caps**. Truncation drops the *newest, lowest-priority* items: issues are listed oldest first, and PRs in review-priority order (fixes, then refactors/docs, then tests, oldest first within each class), so lowering a cap never hides the most urgent work. Raise `max_prs` only if your agents actually work more than 50 PRs per turn; the tail of a long list costs tokens and delivery time on every kick without changing behaviour (#7368). | | `project.agents_md_max_bytes` | `8192`; values under `2048` are raised to `2048`. | Caps the repo `AGENTS.md` block prepended to a kick. Whole sections (split at `#`/`##` headings; each resolved skill is its own unit) are kept or dropped, never clipped mid-rule; when any are dropped the kick carries a one-line `⚠️ AGENTS.md exceeded N bytes; … — read AGENTS.md …` warning naming them and the scheduler logs it. See [agents-md.md](agents-md.md#size-cap-whole-sections-visible-warning). | @@ -431,7 +432,7 @@ advanced into the merge path. | `auto_merge.human_merge_paths` | unset | Per-repo map of `owner/repo` to glob patterns (same syntax as `intent.guardrail_path_patterns`) for paths a person must merge, e.g. `Danathar/goodreads-mcp: [".claude/settings.json", ".claude/hooks/**"]`. Every App merge path honors it: the automerge sweep lanes (App self-merge, trusted-author, label-queued) and the agent merge-request relay refuse a PR that touches a listed path, add `hold`, and post one `<!-- hive-human-merge-path -->` comment naming the path(s); a person must merge it. If the changed-file list cannot be fetched completely for a configured repo, the merge is withheld (#11038, #11039). | | `auto_merge.require_current_base` | unset (off) | Repo list (`owner/repo`) whose App self-merge, trusted-author and contributor lanes only merge a head that contains the current base tip. A green head tested against an older base is updated with GitHub update-branch (a merge commit; no author commit is rewritten) and merges once CI is green on the new head, so a semantic conflict with work merged since turns that PR red instead of breaking the base (#13634). Costs one compare call per merge candidate. | | `auto_merge.merge_forward_trusted_human` | unset (off) | Repo list (`owner/repo`) where a `role/trusted` + `author/human` PR on a same-repo branch that GitHub reports as conflicting (`dirty`) gets the base merged into its head as one App-authored, signed-off merge commit via the GitHub merges API (no human commit is rewritten), then merges once CI is green on the new head. A real conflict pushes nothing and posts one `<!-- hive:merge-forward-conflict head=… -->` note naming the files both sides changed. Fork heads are never written. Attempted once per head (#13636). | -| `auto_merge.merge_train.enabled` | `true` | Conflict-aware merge train in every automerge lane (hive, trusted-author, contributor) ([#15117](https://github.com/hivecommons/hive/issues/15117)). Each sweep groups the open approved / mergeable-pending PRs of one repo+base by overlapping changed files (`GET /pulls/{n}/files`, cached per head SHA) and lets ONE front per group merge — the clean-mergeable PR approved earliest — while the others wait at stage/080 with lane reason `merge-train:queued-behind#<front>:<pos>/<size>:…` (tracker pill "In merge train · 3rd of 4", never a human stage, never `needs-rebase` as a human flag). The moment the front merges, every sibling whose branch is merely `behind` gets `PUT /pulls/{n}/update-branch`; a `dirty` one is handed to the fixer's rebase-resolve episode (`requested_by: merge-train`, the shared two-attempt guard of [#15104](https://github.com/hivecommons/hive/pull/15104); the fixer router still requires a head the hive can push to, so a fork without maintainer edits stays on the plain `conflicting` path for a person). A head the lane last saw blocked on its own (red required check, terminal hold, permanent merge refusal) never fronts, and a group whose best member is blocked imposes no ordering (fail-open) — nobody waits behind a PR that is not moving. One INFO line `merge train` per order change, not per cycle. `false` restores independent per-PR evaluation. | +| `auto_merge.merge_train.enabled` | `true` | Conflict-aware merge train in every automerge lane (hive, trusted-author, contributor) ([#15117](https://github.com/hivecommons/hive/issues/15117)). Each sweep groups the open approved / mergeable-pending PRs of one repo+base by overlapping changed files (`GET /pulls/{n}/files`, cached per head SHA) and lets ONE front per group merge — the clean-mergeable PR approved earliest — while the others wait at stage/080 with lane reason `merge-train:queued-behind#<front>:<pos>/<size>:…` (tracker pill "In merge queue · 3 of 4 · ~4m", never a human stage, never `needs-rebase` as a human flag). The moment the front merges, every sibling whose branch is merely `behind` gets `PUT /pulls/{n}/update-branch`; a `dirty` one is handed to the fixer's rebase-resolve episode (`requested_by: merge-train`, the shared two-attempt guard of [#15104](https://github.com/hivecommons/hive/pull/15104); the fixer router still requires a head the hive can push to, so a fork without maintainer edits stays on the plain `conflicting` path for a person). A head the lane last saw blocked on its own (red required check, terminal hold, permanent merge refusal) never fronts, and a group whose best member is blocked imposes no ordering (fail-open) — nobody waits behind a PR that is not moving. One INFO line `merge train` per order change, not per cycle. The queue carries one PR per issue: when two open PRs declare the same `Fixes #N`, the sweep keeps one (green first, then the earlier-opened) and holds the other with lane reason `merge-train:duplicate-of#<winner>:<issue>/0:0/0:` (tracker pill "Duplicate of #15402 · needs a pick"; *Let hive decide* supersedes, [#15395](https://github.com/hivecommons/hive/issues/15395)). `false` restores independent per-PR evaluation. | | `auto_merge.merge_train.max_rebase_attempts` | `2` | Loop guard: how many rebase-resolve requests the train makes for one PR behind one front. Past it the PR moves to stage/085 with the pill "Needs human decision · rebase failed twice" and the decision option "Rebase, then merge as maintainer". | | `auto_merge.squash_only` | unset (no repo) | Repo list (`owner/repo`) whose branch policy forbids merge commits. Elsewhere, a `role/trusted` PR whose head mixes the contributor's commits with App-linked, App-signed ones (the hive's own in-place adoption fixes), every commit self-signed, is labelled `merge-method/merge` and merged with a merge commit instead of parking at `stage/085` as `adoption-stalled:hive-signoff-attribution`; a listed repo keeps squash-only policy and re-homes an in-place adoption to an App-authored superseder instead (#13975). | | `auto_merge.credit_issue_authors` | `true` | Append `Co-authored-by: <login> <id+login@users.noreply.github.com>` for the human authors of the issues a PR closes (`Fixes|Closes|Resolves #N`) to the squash (or merge) commit message on every hive-performed merge, so reporters land on the contributor graph when an external clanker fixed their issue. Bots, the PR author and logins already credited on the branch are skipped; the logins are recorded as `issue_authors=` on the merge audit entry and in review evidence. One cached `GET /issues/{n}` per newly referenced issue. `false` leaves GitHub's default squash message untouched (#14581). | diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index 3ccbb73bb2..91630dff79 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -1571,10 +1571,16 @@ type GovernorConfig struct { // CIFailureKickCooldownS (default 600) bounds webhook-triggered kicks // to one per PR head in that window. See docs/operator-reference.md // "Webhook-driven CI failure reaction". - CIFailureWebhookKick *bool `yaml:"ci_failure_webhook_kick,omitempty"` - CIFailureKickCooldownS int `yaml:"ci_failure_kick_cooldown_s,omitempty"` - ConserveIntervalMultiplier int `yaml:"conserve_interval_multiplier,omitempty"` - OptionalSweepEveryNCycles int `yaml:"optional_sweep_every_n_cycles,omitempty"` + CIFailureWebhookKick *bool `yaml:"ci_failure_webhook_kick,omitempty"` + CIFailureKickCooldownS int `yaml:"ci_failure_kick_cooldown_s,omitempty"` + // MergeTrainStallWarnS is how long, in seconds, a merge train's head + // may sit without progressing before the Activity Monitor marks the + // train stalled on the queued rows' chips and the dashboard writes one + // activity-log warning per hour (default DefaultMergeTrainStallWarnS, + // hivecommons/hive#15395). + MergeTrainStallWarnS int `yaml:"merge_train_stall_warn_s,omitempty"` + ConserveIntervalMultiplier int `yaml:"conserve_interval_multiplier,omitempty"` + OptionalSweepEveryNCycles int `yaml:"optional_sweep_every_n_cycles,omitempty"` // ExplainMode is the hive-wide default explain mode for agents that leave // their own explain_mode unset. "" means "no hive default configured", in // which case ExplainModeEnvVar is consulted and then off — see @@ -4981,6 +4987,15 @@ func (c *Config) CIFailureKickCooldown() time.Duration { return DefaultCIFailureKickCooldownS * time.Second } +// MergeTrainStallWarn is the effective governor.merge_train_stall_warn_s as +// a duration (default one hour). +func (c *Config) MergeTrainStallWarn() time.Duration { + if c != nil && c.Governor.MergeTrainStallWarnS > 0 { + return time.Duration(c.Governor.MergeTrainStallWarnS) * time.Second + } + return DefaultMergeTrainStallWarnS * time.Second +} + type DashboardIssueBandsConfig struct { WaitingLabels []string `yaml:"waiting_labels,omitempty" json:"waiting_labels,omitempty"` DoneLabels []string `yaml:"done_labels,omitempty" json:"done_labels,omitempty"` @@ -5909,6 +5924,7 @@ const ( DefaultWebhookWakeDebounceS = 10 DefaultWebhookWakeMinGapS = 30 DefaultCIFailureKickCooldownS = 600 + DefaultMergeTrainStallWarnS = 3600 DefaultConserveIntervalMultiplier = 2 DefaultOptionalSweepEveryNCycles = 1 ) diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index 2ed2482d70..d1d58fc263 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -173,6 +173,7 @@ "WebhookWakeMinGapS": 30, "CIFailureWebhookKick": null, "CIFailureKickCooldownS": 0, + "MergeTrainStallWarnS": 0, "ConserveIntervalMultiplier": 2, "OptionalSweepEveryNCycles": 1, "ExplainMode": "", diff --git a/src/pkg/config/validate.go b/src/pkg/config/validate.go index 0c251f7d1d..8bedaf9d02 100644 --- a/src/pkg/config/validate.go +++ b/src/pkg/config/validate.go @@ -280,6 +280,9 @@ func (c *Config) ValidateWithOptions(opts ValidateOptions) error { if c.Governor.CIFailureKickCooldownS < 0 { return fmt.Errorf("governor.ci_failure_kick_cooldown_s must be at least 0") } + if c.Governor.MergeTrainStallWarnS < 0 { + return fmt.Errorf("governor.merge_train_stall_warn_s must be at least 0") + } if c.Governor.ConserveIntervalMultiplier < 0 { return fmt.Errorf("governor.conserve_interval_multiplier must be at least 1") } diff --git a/src/pkg/dashboard/delegated_pr_context.go b/src/pkg/dashboard/delegated_pr_context.go index 0d9465eaa7..968fe1b9e7 100644 --- a/src/pkg/dashboard/delegated_pr_context.go +++ b/src/pkg/dashboard/delegated_pr_context.go @@ -12,6 +12,8 @@ import ( "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/delegatedecision" "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stageengine/prstage" + "github.com/hivecommons/hive/pkg/stagelabels" ) // errDelegatedNoSnapshot fails a delegated PR decision closed when the @@ -120,15 +122,36 @@ func (s *Server) gatherDelegatedPRContext(ctx context.Context, repo string, pr * } mergeable = len(summary.Red) == 0 && summary.Pending == 0 && summary.Green > 0 } + winner, _ := delegatedTrainDuplicate(s.stageStoreOrShared(), repo, number) in.Competing = append(in.Competing, delegatedecision.CompetingPR{ Number: number, HiveAuthored: s.deps.GHClient.IsHiveAuthorLogin(other.GetUser().GetLogin()), - Mergeable: mergeable, - Current: state != "behind" && state != "unknown" && state != "" && !other.GetDraft(), + Mergeable: mergeable, + Current: state != "behind" && state != "unknown" && state != "" && !other.GetDraft(), + HeldDuplicate: winner == pr.GetNumber(), }) } + in.TrainDuplicateOf, in.TrainDuplicateIssue = delegatedTrainDuplicate(s.stageStoreOrShared(), repo, pr.GetNumber()) return nil } +// delegatedTrainDuplicate reads the stage store for the one-PR-per-issue +// hold the merge queue recorded on a PR (hivecommons/hive#15395): the PR +// it yields to and the shared issue, 0 when it is not held. +func delegatedTrainDuplicate(store *stagelabels.Store, repo string, number int) (winner, issue int) { + if store == nil { + return 0, 0 + } + rec, ok := store.Get(stagelabels.Key(repo, number)) + if !ok { + return 0, 0 + } + mt, ok := prstage.ParseMergeTrain(rec.Reason) + if !ok || mt.State != prstage.MergeTrainStateDuplicateOf { + return 0, 0 + } + return mt.Front, mt.Position +} + // delegatedActivity is one author-attributed event on a PR. type delegatedActivity struct { Login string diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index d2ec4403d3..5f67f89a78 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -104,6 +104,11 @@ type Server struct { feedbackRefreshBackoff feedbackBackoff + // mergeTrains remembers the trains between tracker builds: the hourly + // "merge train stalled" activity-log line per train and each member's + // first estimate (stage_tracker_merge_train.go). + mergeTrains mergeTrainMemory + // chatGroundedInflight caps concurrent grounded chat turns // (api_chat_grounded.go); created lazily by chatGroundedSlots. chatGroundedOnce sync.Once diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 2d2968c5c6..0ac1cd0a27 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -2329,7 +2329,8 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto } } resp.OpenIssues, resp.OpenPRs = stageTrackerOpenCounts(resp.Items) - stageTrackerMergeTrainPostPass(resp.Items) + stageTrackerMergeTrainPostPass(resp.Items, mergeTrainQueueOptionsFrom(cfg, sweep, now), &s.mergeTrains) + stageTrackerMergeTrainStallWarnings(&s.mergeTrains, resp.Items, now) stageTrackerLineageFillAuthors(resp.Items) s.applyOperatorActions(resp.Items, now) s.applyDecidedProjection(resp.Items, now) diff --git a/src/pkg/dashboard/stage_tracker_duplicate_hold_test.go b/src/pkg/dashboard/stage_tracker_duplicate_hold_test.go new file mode 100644 index 0000000000..b0d58d4ee7 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_duplicate_hold_test.go @@ -0,0 +1,60 @@ +package dashboard + +import ( + "slices" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/stageengine/prstage" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// A PR the sweep held out of the merge queue as a duplicate +// (hivecommons/hive#15395) reads "duplicate-of:<winner>" on the gate with +// the winner and issue beside it, rides no train, and wears +// needs-decision so the row offers "Let hive decide". +func TestStageTrackerApplyDuplicateHold(t *testing.T) { + reason := prstage.MergeTrain{State: prstage.MergeTrainStateDuplicateOf, Front: 15402, Position: 15396}.Reason() + it := StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15407, Stage: 80, BaseRef: "v5", Reason: reason, Modifiers: []string{modNeedsRebase}} + stageTrackerApplyMergeTrain(&it) + if it.MergeTrain != nil { + t.Fatalf("a held duplicate rides no train: %+v", it.MergeTrain) + } + if it.MergeGate == nil || it.MergeGate.State != "duplicate-of:15402" || it.MergeGate.DuplicateOf != 15402 || it.MergeGate.DuplicateIssue != 15396 { + t.Fatalf("gate = %+v", it.MergeGate) + } + if it.MergeGate.Reason != "duplicate of #15402 for issue #15396; the merge queue carries one PR per issue — a person or the delegate picks one" { + t.Fatalf("reason = %q", it.MergeGate.Reason) + } + if !slices.Contains(it.Modifiers, modNeedsDecision) || !slices.Contains(it.Modifiers, modNeedsRebase) { + t.Fatalf("modifiers = %v", it.Modifiers) + } + // Applying twice does not stack the modifier; the post-pass ignores it. + stageTrackerApplyMergeTrain(&it) + if n := slices.Index(it.Modifiers, modNeedsDecision); n < 0 || slices.Contains(it.Modifiers[n+1:], modNeedsDecision) { + t.Fatalf("modifiers = %v", it.Modifiers) + } + items := []StageTrackerItem{it} + stageTrackerMergeTrainPostPass(items, mergeTrainQueueOptions{Now: time.Now()}, &mergeTrainMemory{}) + if items[0].MergeTrain != nil { + t.Fatalf("post-pass gave the held row a train: %+v", items[0].MergeTrain) + } +} + +func TestDelegatedTrainDuplicateReadsStore(t *testing.T) { + now := time.Date(2026, 10, 29, 15, 0, 0, 0, time.UTC) + store := stagelabels.NewStore("", func() time.Time { return now }) + store.Observe(stagelabels.Key("hivecommons/hive", 15407), stagelabels.AwaitingMergeAdmission, + prstage.MergeTrain{State: prstage.MergeTrainStateDuplicateOf, Front: 15402, Position: 15396}.Reason()) + store.Observe(stagelabels.Key("hivecommons/hive", 15402), stagelabels.AwaitingMergeAdmission, + prstage.MergeTrain{State: prstage.MergeTrainStateQueuedBehind, Front: 15315, Position: 2, Size: 3}.Reason()) + if w, i := delegatedTrainDuplicate(store, "hivecommons/hive", 15407); w != 15402 || i != 15396 { + t.Fatalf("held = #%d issue #%d", w, i) + } + if w, i := delegatedTrainDuplicate(store, "hivecommons/hive", 15402); w != 0 || i != 0 { + t.Fatalf("a queued member is not held: #%d #%d", w, i) + } + if w, _ := delegatedTrainDuplicate(nil, "hivecommons/hive", 15407); w != 0 { + t.Fatal("nil store") + } +} diff --git a/src/pkg/dashboard/stage_tracker_merge_gate.go b/src/pkg/dashboard/stage_tracker_merge_gate.go index 80f0f3648c..9ed6bbe770 100644 --- a/src/pkg/dashboard/stage_tracker_merge_gate.go +++ b/src/pkg/dashboard/stage_tracker_merge_gate.go @@ -7,6 +7,7 @@ import ( "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/github/automerge" + "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -77,6 +78,22 @@ type StageTrackerMergeGate struct { MergeFailedKind string `json:"merge_failed_kind,omitempty"` MergeFailedText string `json:"merge_failed_text,omitempty"` MergeFailedChecks int `json:"merge_failed_checks,omitempty"` + // DuplicateOf / DuplicateIssue: for State "duplicate-of:<n>", the open + // PR the merge queue kept for the same issue and that issue + // (hivecommons/hive#15395); the row waits for a person or the + // delegate to pick one. + DuplicateOf int `json:"duplicate_of,omitempty"` + DuplicateIssue int `json:"duplicate_issue,omitempty"` +} + +// MergeGateStateDuplicateOfPrefix opens the gate state of a PR held out of +// the merge queue because another open PR fixes the same issue: +// "duplicate-of:15402". +const MergeGateStateDuplicateOfPrefix = prstage.MergeTrainStateDuplicateOf + ":" + +// MergeGateStateDuplicateOf is the gate state for a PR yielding to n. +func MergeGateStateDuplicateOf(n int) string { + return MergeGateStateDuplicateOfPrefix + strconv.Itoa(n) } // stageTrackerMergeGateState maps a stage to its merge-gate state; "" off diff --git a/src/pkg/dashboard/stage_tracker_merge_gate_test.go b/src/pkg/dashboard/stage_tracker_merge_gate_test.go index 0d6fb96803..068ed4d23c 100644 --- a/src/pkg/dashboard/stage_tracker_merge_gate_test.go +++ b/src/pkg/dashboard/stage_tracker_merge_gate_test.go @@ -301,9 +301,37 @@ func stageTrackerMergeGateInfoJS(t *testing.T, html string) string { jsFunc(t, html, "stageTrackerCoveringPR") + "\n" + jsFunc(t, html, "stageTrackerMergeGateInfo") + "\n" + jsFunc(t, html, "stageTrackerMergeGateInfoFor") + "\n" + + stageTrackerTrainQueueJS(t, html) + "\n" + stageTrackerSoakClockJS(t, html) } +// stageTrackerTrainQueueJS lifts the merge-queue words the gate pill reads +// for a train member (hivecommons/hive#15395). +func stageTrackerTrainQueueJS(t *testing.T, html string) string { + t.Helper() + return strings.Join([]string{ + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_FRONT"), + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_QUEUED"), + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_REBASING"), + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_EXHAUSTED"), + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_WORD"), + jsConstLine(t, html, "STAGE_TRACKER_GATE_DUPLICATE_PREFIX"), + jsFunc(t, html, "stageTrackerGateDuplicateOf"), + jsFunc(t, html, "stageTrackerMergeGateStateWord"), + jsFunc(t, html, "stageTrackerTrain"), + jsFunc(t, html, "stageTrackerOrdinal"), + jsFunc(t, html, "stageTrackerShortLeft"), + jsFunc(t, html, "stageTrackerTrainPlace"), + jsFunc(t, html, "stageTrackerTrainStalledWord"), + jsFunc(t, html, "stageTrackerTrainWord"), + jsFunc(t, html, "stageTrackerTrainEtaAt"), + jsFunc(t, html, "stageTrackerTrainEtaWords"), + jsFunc(t, html, "stageTrackerTrainEtaDetail"), + jsFunc(t, html, "stageTrackerTrainQueueTip"), + jsFunc(t, html, "stageTrackerTrainLines"), + }, "\n") +} + // stageTrackerSoakClockJS lifts the soak-clock helpers the gate pill reads. func stageTrackerSoakClockJS(t *testing.T, html string) string { t.Helper() diff --git a/src/pkg/dashboard/stage_tracker_merge_train.go b/src/pkg/dashboard/stage_tracker_merge_train.go index 7dfa05cd33..fa895ccc2d 100644 --- a/src/pkg/dashboard/stage_tracker_merge_train.go +++ b/src/pkg/dashboard/stage_tracker_merge_train.go @@ -1,11 +1,16 @@ package dashboard import ( + "log/slog" "slices" "sort" "strconv" "strings" + "sync" + "time" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github/automerge" "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -50,6 +55,71 @@ type StageTrackerMergeTrain struct { OverlapFiles []string `json:"overlap_files,omitempty"` // Ahead lists the PRs still to merge before this one, in order. Ahead []int `json:"ahead,omitempty"` + // Queue facts for the chip (hivecommons/hive#15395): Length repeats + // Size and HeadNumber repeats Front under the names the SPA's queue + // wording uses; HeadState is what the head is doing now (one of the + // mergeTrainHead* values) and HeadReason says it in plain words; + // HeadSince is when the head entered its current stage; EtaS is the + // estimated seconds until this member's own merge attempt (0 when no + // estimate is possible — the head waits on a person or on CI), EtaAt + // the same as a wall-clock instant for a countdown that survives + // re-renders, EtaBasis its short reading ("2 ahead · next sweep in + // 40s") and Late true once the turn has overrun its first estimate + // twice over; + // Stalled is true once the head has sat longer than + // governor.merge_train_stall_warn_s, StalledS for how long. + Length int `json:"length,omitempty"` + HeadNumber int `json:"head_number,omitempty"` + HeadState string `json:"head_state,omitempty"` + HeadReason string `json:"head_reason,omitempty"` + HeadSince *time.Time `json:"head_since,omitempty"` + EtaS int64 `json:"eta_s,omitempty"` + EtaAt *time.Time `json:"eta_at,omitempty"` + EtaBasis string `json:"eta_basis,omitempty"` + Late bool `json:"late,omitempty"` + Stalled bool `json:"stalled,omitempty"` + StalledS int64 `json:"stalled_s,omitempty"` +} + +// Head states a merge train reports on its members (HeadState). The SPA +// words them; the API carries the token. +const ( + // mergeTrainHeadNeedsHuman: the head waits on a person (090 ready for + // a human merge, 085 needs decision, a hold). + mergeTrainHeadNeedsHuman = "needs-human" + // mergeTrainHeadCI: the head's checks are still running or red (060). + mergeTrainHeadCI = "ci" + // mergeTrainHeadSoaking: the head is admitted and soaking (095, or a + // soak clock at 080). + mergeTrainHeadSoaking = "soaking" + // mergeTrainHeadReady: approved and green, waiting for the next sweep. + mergeTrainHeadReady = "ready" + // mergeTrainHeadMerging: the merge call is in flight (100). + mergeTrainHeadMerging = "merging" + // mergeTrainHeadFixing: an agent is on the head (070/071/072). + mergeTrainHeadFixing = "fixing" + // mergeTrainHeadUnknown: the head is not in this payload (closed, + // filtered, another page); the sibling only knows its number. + mergeTrainHeadUnknown = "unknown" +) + +// mergeTrainQueueOptions tunes the queue facts: Now for the clocks, Sweep +// for one sweep's worth of wait per PR ahead with no soak clock of its +// own (the lanes' floor cadence), NextSweepAt for the tick the estimate +// ends on, StallWarn for how long a head may sit before the train reads +// stalled. +type mergeTrainQueueOptions struct { + Now time.Time + Sweep time.Duration + NextSweepAt time.Time + StallWarn time.Duration +} + +// mergeTrainQueueOptionsFrom reads the options from the config and the +// lanes' live sweep clock: governor.merge_train_stall_warn_s is the stall +// threshold, the sweep's floor cadence the unit per PR ahead. +func mergeTrainQueueOptionsFrom(cfg *config.Config, sweep automerge.SweepState, now time.Time) mergeTrainQueueOptions { + return mergeTrainQueueOptions{Now: now, Sweep: sweep.FloorEvery, NextSweepAt: sweep.NextFloorAt, StallWarn: cfg.MergeTrainStallWarn()} } // stageTrackerMergedStage is the terminal strip number (the dashboard's @@ -87,6 +157,10 @@ func stageTrackerApplyMergeTrain(it *StageTrackerItem) { if mt == nil { return } + if mt.State == prstage.MergeTrainStateDuplicateOf { + stageTrackerApplyDuplicateHold(it, mt) + return + } mt.ID = it.BaseRef + "#" + strconv.Itoa(mt.Front) it.MergeTrain = mt parsed, _ := prstage.ParseMergeTrain(it.Reason) @@ -103,11 +177,31 @@ func stageTrackerApplyMergeTrain(it *StageTrackerItem) { } } +// stageTrackerApplyDuplicateHold decorates a PR the sweep held out of the +// merge queue because another open PR fixes the same issue +// (hivecommons/hive#15395): the gate reads "duplicate-of:<winner>" with +// the winner and the issue on the gate, the row rides no train, and it +// wears needs-decision so "Let hive decide" offers the supersede path. +func stageTrackerApplyDuplicateHold(it *StageTrackerItem, mt *StageTrackerMergeTrain) { + parsed, _ := prstage.ParseMergeTrain(it.Reason) + if it.MergeGate == nil { + it.MergeGate = &StageTrackerMergeGate{} + } + it.MergeGate.State = MergeGateStateDuplicateOf(mt.Front) + it.MergeGate.Reason = parsed.Sentence() + it.MergeGate.DuplicateOf = mt.Front + it.MergeGate.DuplicateIssue = mt.Position + it.MergeTrain = nil + if !slices.Contains(it.Modifiers, modNeedsDecision) { + it.Modifiers = append(it.Modifiers, modNeedsDecision) + } +} + // stageTrackerMergeTrainPostPass runs over the finished items: the front // (which the lane evaluates on its normal path and so carries no train // reason) is marked from its siblings' records, and every member learns // the PRs ahead of it. Members are matched on repo and base. -func stageTrackerMergeTrainPostPass(items []StageTrackerItem) { +func stageTrackerMergeTrainPostPass(items []StageTrackerItem, opts mergeTrainQueueOptions, mem *mergeTrainMemory) { type trainKey struct{ repo, id string } members := map[trainKey][]int{} for i := range items { @@ -158,6 +252,333 @@ func stageTrackerMergeTrainPostPass(items []StageTrackerItem) { } items[i].MergeTrain.Ahead = ahead } + var head *StageTrackerItem + if frontIdx >= 0 { + head = &items[frontIdx] + } + for n, i := range idx { + ahead := make([]*StageTrackerItem, 0, n) + for _, j := range idx[:n] { + ahead = append(ahead, &items[j]) + } + stageTrackerMergeTrainQueueFacts(&items[i], ahead, head, opts, mem) + } + } +} + +// stageTrackerMergeTrainQueueFacts fills the chip's queue facts on one +// member: what the head is doing, how long it has sat there, the estimate +// of this member's own turn and whether the train reads stalled. ahead is +// every member before this one in merge order (the head first, when it is +// in the view); mem remembers each member's first estimate so a turn that +// has overrun it twice over reads "running late". +func stageTrackerMergeTrainQueueFacts(self *StageTrackerItem, ahead []*StageTrackerItem, head *StageTrackerItem, opts mergeTrainQueueOptions, mem *mergeTrainMemory) { + if self == nil || self.MergeTrain == nil { + return + } + mt := self.MergeTrain + mt.Length = mt.Size + mt.HeadNumber = mt.Front + state, reason, headLeft, known := mergeTrainHeadState(head, opts.Now) + mt.HeadState, mt.HeadReason = state, reason + if head != nil && head.StageSince != nil && !head.StageSince.IsZero() { + since := *head.StageSince + mt.HeadSince = &since + } + mt.EtaS, mt.EtaAt, mt.EtaBasis, mt.Late = 0, nil, "", false + // Stalled: the head has sat in its stage past the warn window and is + // not actually merging. A soaking head is on its clock, not stalled, + // until that clock has itself overrun the window. + if opts.StallWarn > 0 && mt.HeadSince != nil && state != mergeTrainHeadMerging && state != mergeTrainHeadUnknown { + sat := opts.Now.Sub(*mt.HeadSince) + if sat > opts.StallWarn && !(state == mergeTrainHeadSoaking && headLeft > 0) { + mt.Stalled = true + mt.StalledS = int64(sat / time.Second) + } + } + // No estimate while the head (or this member) waits on a person or on + // CI: the blocker is the word, not a clock. + if !known || self.NeedsHuman || self.Hold != nil { + if mem != nil { + mem.forgetEta(self) + } + return + } + // The estimate (hivecommons/hive#15395): this member's own soak left, + // plus each PR ahead — its remaining soak, or one sweep when it has + // none — plus the wait for the next sweep tick. + sweep := opts.Sweep + if sweep <= 0 { + sweep = mergeTrainDefaultSweepEvery + } + var eta time.Duration + own := stageTrackerSoakLeftOf(self, opts.Now) + eta += own + for i, a := range ahead { + left := stageTrackerSoakLeftOf(a, opts.Now) + if i == 0 && a == head { + left = headLeft + } + if left <= 0 { + left = sweep + } + eta += left + } + tick := opts.NextSweepIn(self) + eta += tick + if eta < time.Second { + eta = time.Second + } + mt.EtaS = int64(eta / time.Second) + at := opts.Now.Add(eta) + mt.EtaBasis = mergeTrainEtaBasis(len(ahead), state, headLeft, own, tick, self.MergeGate != nil && self.MergeGate.SweepRunning) + if mem != nil { + at, mt.Late = mem.etaAnchor(self, mt, at, opts.Now, sweep) + } + mt.EtaAt = &at +} + +// mergeTrainDefaultSweepEvery stands in for the lanes' floor cadence +// before the sweep loop has armed it (automerge's floor interval). +const mergeTrainDefaultSweepEvery = 5 * time.Minute + +// NextSweepIn is how long until the next sweep tick: the member's own +// next_sweep_at when the gate carries one, else the lanes' floor; 0 while +// a sweep runs or none is armed. +func (o mergeTrainQueueOptions) NextSweepIn(self *StageTrackerItem) time.Duration { + if self != nil && self.MergeGate != nil { + if self.MergeGate.SweepRunning { + return 0 + } + if self.MergeGate.NextSweepAt != nil && self.MergeGate.NextSweepAt.After(o.Now) { + return self.MergeGate.NextSweepAt.Sub(o.Now) + } + } + if !o.NextSweepAt.IsZero() && o.NextSweepAt.After(o.Now) { + return o.NextSweepAt.Sub(o.Now) + } + return 0 +} + +// stageTrackerSoakLeftOf is how much of an item's own soak clock remains. +func stageTrackerSoakLeftOf(it *StageTrackerItem, now time.Time) time.Duration { + if it == nil { + return 0 + } + if it.MergeGate != nil && it.MergeGate.SoakRemainingSeconds > 0 { + return time.Duration(it.MergeGate.SoakRemainingSeconds) * time.Second + } + if it.Soak != nil && it.Soak.Until.After(now) { + return it.Soak.Until.Sub(now) + } + return 0 +} + +// mergeTrainEtaBasis is the short plain-English reading of the estimate: +// "2 ahead · head soaking 12m · next sweep in 40s". +func mergeTrainEtaBasis(aheadN int, headState string, headLeft, own, tick time.Duration, sweeping bool) string { + var parts []string + switch aheadN { + case 0: + parts = append(parts, "front") + case 1: + parts = append(parts, "1 ahead") + default: + parts = append(parts, strconv.Itoa(aheadN)+" ahead") + } + if aheadN > 0 && headState == mergeTrainHeadSoaking && headLeft > 0 { + parts = append(parts, "head soaking "+shortDuration(headLeft)) + } else if aheadN > 0 && headState == mergeTrainHeadMerging { + parts = append(parts, "head merging") + } + if own > 0 { + parts = append(parts, "own soak "+shortDuration(own)) + } + if sweeping { + parts = append(parts, "sweep running") + } else if tick > 0 { + parts = append(parts, "next sweep in "+shortDuration(tick)) + } + return strings.Join(parts, " · ") +} + +// mergeTrainHeadState reads the head's current state, its plain-word +// reason, how long its own clock still runs (soak left, next sweep) and +// whether that clock is known at all (false on a person or CI). +func mergeTrainHeadState(head *StageTrackerItem, now time.Time) (state, reason string, remaining time.Duration, known bool) { + if head == nil { + return mergeTrainHeadUnknown, "the head of the train is not in this view", 0, false + } + mg := head.MergeGate + if mg != nil && mg.State == MergeGateStateMerging { + return mergeTrainHeadMerging, "merging now", 0, true + } + if mg != nil && mg.State == MergeGateStateMergeFailed { + return mergeTrainHeadReady, "GitHub refused the last merge; hive retries on its next sweep", 0, false + } + switch head.Stage { + case trackerNumberReadyForHuman: + why := "waiting for a maintainer" + if head.HumanMergeReason != "" { + why += " (" + strings.ReplaceAll(head.HumanMergeReason, "-", " ") + ")" + } + return mergeTrainHeadNeedsHuman, why, 0, false + case trackerNumberNeedsDecision, trackerNumberIssueDecision: + return mergeTrainHeadNeedsHuman, "waiting for a human decision", 0, false + case stagelabels.MergeQueue.Number(): + return mergeTrainHeadMerging, "merging now", 0, true + case trackerNumberPreMergeSoak: + left := time.Duration(0) + if mg != nil && mg.SoakRemainingSeconds > 0 { + left = time.Duration(mg.SoakRemainingSeconds) * time.Second + } + if left <= 0 && head.Soak != nil && head.Soak.Until.After(now) { + left = head.Soak.Until.Sub(now) + } + if left > 0 { + return mergeTrainHeadSoaking, "soaking, " + shortDuration(left) + " left", left, true + } + return mergeTrainHeadSoaking, "soaked; hive merges it on its next sweep", 0, true + case trackerNumberReviewed, trackerNumberReviewPending: + return mergeTrainHeadCI, "waiting for CI", 0, false + case trackerNumberFixing, trackerNumberReturned, trackerNumberNeedsAuthor: + return mergeTrainHeadFixing, "being fixed by its author", 0, false + } + if head.Hold != nil || head.NeedsHuman { + return mergeTrainHeadNeedsHuman, "waiting for a maintainer", 0, false + } + if head.Soak != nil && head.Soak.Until.After(now) { + left := head.Soak.Until.Sub(now) + return mergeTrainHeadSoaking, "soaking, " + shortDuration(left) + " left", left, true + } + var next time.Duration + if mg != nil && mg.NextSweepAt != nil && mg.NextSweepAt.After(now) { + next = mg.NextSweepAt.Sub(now) + } + return mergeTrainHeadReady, "approved and green; merges on hive's next sweep", next, true +} + +// shortDuration is "12m" / "1h 05m" / "45s" for the chip's words. +func shortDuration(d time.Duration) string { + d = d.Round(time.Second) + if d < time.Minute { + return strconv.Itoa(int(d/time.Second)) + "s" + } + m := int(d / time.Minute) + if m < 60 { + return strconv.Itoa(m) + "m" + } + rem := m % 60 + if rem == 0 { + return strconv.Itoa(m/60) + "h" + } + rest := strconv.Itoa(rem) + if rem < 10 { + rest = "0" + rest + } + return strconv.Itoa(m/60) + "h " + rest + "m" +} + +// mergeTrainMemory is what the tracker keeps between builds for the +// trains: the hour-stamp of each stalled train's activity-log line and +// each member's first estimate (for "running late"). +type mergeTrainMemory struct { + mu sync.Mutex + last map[string]time.Time + eta map[string]mergeTrainEtaMark +} + +// mergeTrainEtaMark is a member's first estimate at a given position. +type mergeTrainEtaMark struct { + At time.Time + Eta time.Duration + Position int +} + +const mergeTrainStallLogEvery = time.Hour + +// mergeTrainLateFactor: a turn still pending this many times past its +// first estimate reads "running late". +const mergeTrainLateFactor = 2 + +func mergeTrainMemberKey(it *StageTrackerItem) string { + return it.Repo + "|" + strconv.Itoa(it.Number) +} + +// etaAnchor keeps a member's countdown steady across builds: the first +// estimate at a position is the anchor the SPA counts down to, and a +// re-estimate only moves it when it runs more than one sweep longer (the +// head's clock grew) — otherwise every poll would push the end out by the +// sweep it just waited. It also reports "running late": now past the +// first estimate mergeTrainLateFactor times over. +func (m *mergeTrainMemory) etaAnchor(self *StageTrackerItem, mt *StageTrackerMergeTrain, fresh, now time.Time, sweep time.Duration) (time.Time, bool) { + if m == nil || self == nil || mt == nil || mt.EtaS <= 0 { + return fresh, false + } + m.mu.Lock() + defer m.mu.Unlock() + if m.eta == nil { + m.eta = map[string]mergeTrainEtaMark{} + } + key := mergeTrainMemberKey(self) + mark, ok := m.eta[key] + if !ok || mark.Position != mt.Position || fresh.Sub(now) > mark.Eta+sweep { + m.eta[key] = mergeTrainEtaMark{At: now, Eta: time.Duration(mt.EtaS) * time.Second, Position: mt.Position} + return fresh, false + } + anchor := mark.At.Add(mark.Eta) + if anchor.Before(now) { + anchor = fresh + } + return anchor, now.Sub(mark.At) > mergeTrainLateFactor*mark.Eta +} + +// forgetEta drops the member's mark: a blocked member starts a fresh +// estimate once it is unblocked. +func (m *mergeTrainMemory) forgetEta(self *StageTrackerItem) { + if m == nil || self == nil { + return + } + m.mu.Lock() + delete(m.eta, mergeTrainMemberKey(self)) + m.mu.Unlock() +} + +// warn logs the stalled train once per hour; it returns true when it logged. +func (m *mergeTrainMemory) warn(repo string, mt *StageTrackerMergeTrain, now time.Time) bool { + if m == nil || mt == nil || !mt.Stalled { + return false + } + m.mu.Lock() + defer m.mu.Unlock() + if m.last == nil { + m.last = map[string]time.Time{} + } + key := repo + "|" + mt.ID + if at, ok := m.last[key]; ok && now.Sub(at) < mergeTrainStallLogEvery { + return false + } + m.last[key] = now + slog.Warn("merge train stalled: head has not progressed; "+strconv.Itoa(mt.Size-1)+" PR(s) wait behind it", + "repo", repo, "train", mt.ID, "head", mt.HeadNumber, "head_state", mt.HeadState, "head_reason", mt.HeadReason, "stalled_for", shortDuration(time.Duration(mt.StalledS)*time.Second), "size", mt.Size) + return true +} + +// stageTrackerMergeTrainStallWarnings runs the hourly log over the items: +// one line per stalled train, read from any member. +func stageTrackerMergeTrainStallWarnings(mem *mergeTrainMemory, items []StageTrackerItem, now time.Time) { + seen := map[string]bool{} + for i := range items { + mt := items[i].MergeTrain + if mt == nil || !mt.Stalled { + continue + } + key := items[i].Repo + "|" + mt.ID + if seen[key] { + continue + } + seen[key] = true + mem.warn(items[i].Repo, mt, now) } } diff --git a/src/pkg/dashboard/stage_tracker_merge_train_queue_test.go b/src/pkg/dashboard/stage_tracker_merge_train_queue_test.go new file mode 100644 index 0000000000..6411864e03 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_merge_train_queue_test.go @@ -0,0 +1,397 @@ +package dashboard + +import ( + "bytes" + "encoding/json" + "log/slog" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/stageengine/prstage" +) + +// The chip's queue facts (hivecommons/hive#15395): position / length, the +// head's state in plain words, the estimate and its basis, the stall flag. +func TestStageTrackerMergeTrainQueueFacts(t *testing.T) { + now := time.Date(2026, 10, 29, 15, 0, 0, 0, time.UTC) + opts := mergeTrainQueueOptions{Now: now, Sweep: 5 * time.Minute, NextSweepAt: now.Add(40 * time.Second), StallWarn: time.Hour} + since := func(ago time.Duration) *time.Time { at := now.Add(-ago); return &at } + member := func(n, pos int, extra func(*StageTrackerItem)) *StageTrackerItem { + it := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: n, Stage: trackerNumberAwaitingMerge, + MergeTrain: &StageTrackerMergeTrain{ID: "v5#15315", State: MergeGateStateQueuedBehind, Front: 15315, Position: pos, Size: 3}} + if extra != nil { + extra(it) + } + return it + } + soaking := func(left time.Duration) func(*StageTrackerItem) { + return func(it *StageTrackerItem) { + it.MergeGate = &StageTrackerMergeGate{State: "soaking", SoakRemainingSeconds: int64(left / time.Second)} + } + } + headSoaking := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15315, Stage: trackerNumberPreMergeSoak, StageSince: since(10 * time.Minute), + MergeGate: &StageTrackerMergeGate{State: "soaking", SoakRemainingSeconds: 12 * 60}} + headHuman := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15315, Stage: trackerNumberReadyForHuman, StageSince: since(8 * time.Hour), HumanMergeReason: "untrusted-author", + MergeGate: &StageTrackerMergeGate{State: MergeGateStateTrainFront}} + headCI := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15315, Stage: trackerNumberReviewed, StageSince: since(3 * time.Hour)} + headMerging := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15315, Stage: trackerNumberAwaitingMerge, StageSince: since(3 * time.Hour), MergeGate: &StageTrackerMergeGate{State: MergeGateStateMerging}} + headReady := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15315, Stage: trackerNumberAwaitingMerge, StageSince: since(20 * time.Minute), MergeGate: &StageTrackerMergeGate{State: MergeGateStateTrainFront}} + + cases := []struct { + name string + self *StageTrackerItem + ahead []*StageTrackerItem + head *StageTrackerItem + opts mergeTrainQueueOptions + headState string + reason string + etaS int64 + basis string + stalled bool + stalledS int64 + }{ + {name: "2nd behind a soaking head: head soak + next tick", self: member(15319, 2, nil), ahead: []*StageTrackerItem{headSoaking}, head: headSoaking, opts: opts, + headState: mergeTrainHeadSoaking, reason: "soaking, 12m left", etaS: 12*60 + 40, basis: "1 ahead · head soaking 12m · next sweep in 40s"}, + {name: "3rd: head soak + one sweep for the 2nd + tick", self: member(15374, 3, nil), ahead: []*StageTrackerItem{headSoaking, member(15319, 2, nil)}, head: headSoaking, opts: opts, + headState: mergeTrainHeadSoaking, reason: "soaking, 12m left", etaS: 12*60 + 5*60 + 40, basis: "2 ahead · head soaking 12m · next sweep in 40s"}, + {name: "3rd with a soaking 2nd: its soak, not a sweep", self: member(15374, 3, nil), ahead: []*StageTrackerItem{headSoaking, member(15319, 2, soaking(7*time.Minute))}, head: headSoaking, opts: opts, + headState: mergeTrainHeadSoaking, reason: "soaking, 12m left", etaS: 12*60 + 7*60 + 40, basis: "2 ahead · head soaking 12m · next sweep in 40s"}, + {name: "own soak counts too", self: member(15319, 2, soaking(3*time.Minute)), ahead: []*StageTrackerItem{headSoaking}, head: headSoaking, opts: opts, + headState: mergeTrainHeadSoaking, reason: "soaking, 12m left", etaS: 12*60 + 3*60 + 40, basis: "1 ahead · head soaking 12m · own soak 3m · next sweep in 40s"}, + {name: "a running sweep has no tick", self: member(15319, 2, func(it *StageTrackerItem) { + it.MergeGate = &StageTrackerMergeGate{State: MergeGateStateQueuedBehind, SweepRunning: true} + }), ahead: []*StageTrackerItem{headSoaking}, head: headSoaking, opts: opts, + headState: mergeTrainHeadSoaking, reason: "soaking, 12m left", etaS: 12 * 60, basis: "1 ahead · head soaking 12m · sweep running"}, + {name: "ready head: one sweep each + tick", self: member(15374, 3, nil), ahead: []*StageTrackerItem{headReady, member(15319, 2, nil)}, head: headReady, opts: opts, + headState: mergeTrainHeadReady, reason: "approved and green; merges on hive's next sweep", etaS: 2*5*60 + 40, basis: "2 ahead · next sweep in 40s"}, + {name: "no cadence armed yet: the default floor", self: member(15319, 2, nil), ahead: []*StageTrackerItem{headReady}, head: headReady, opts: mergeTrainQueueOptions{Now: now, StallWarn: time.Hour}, + headState: mergeTrainHeadReady, reason: "approved and green; merges on hive's next sweep", etaS: int64(mergeTrainDefaultSweepEvery / time.Second), basis: "1 ahead"}, + {name: "merging head: its turn is one sweep away", self: member(15319, 2, nil), ahead: []*StageTrackerItem{headMerging}, head: headMerging, opts: opts, + headState: mergeTrainHeadMerging, reason: "merging now", etaS: 5*60 + 40, basis: "1 ahead · head merging · next sweep in 40s"}, + {name: "head on a human: blocker, no estimate, stalled past the window", self: member(15319, 2, nil), ahead: []*StageTrackerItem{headHuman}, head: headHuman, opts: opts, + headState: mergeTrainHeadNeedsHuman, reason: "waiting for a maintainer (untrusted author)", stalled: true, stalledS: 8 * 3600}, + {name: "head on CI: blocker, no estimate", self: member(15319, 2, nil), ahead: []*StageTrackerItem{headCI}, head: headCI, opts: opts, + headState: mergeTrainHeadCI, reason: "waiting for CI", stalled: true, stalledS: 3 * 3600}, + {name: "head not in view: unknown, no estimate, no stall", self: member(15319, 2, nil), ahead: nil, head: nil, opts: opts, + headState: mergeTrainHeadUnknown, reason: "the head of the train is not in this view"}, + {name: "stall_warn 0 turns the warning off", self: member(15319, 2, nil), ahead: []*StageTrackerItem{headHuman}, head: headHuman, opts: mergeTrainQueueOptions{Now: now, Sweep: opts.Sweep}, + headState: mergeTrainHeadNeedsHuman, reason: "waiting for a maintainer (untrusted author)"}, + {name: "a member on a person has no estimate either", self: member(15319, 2, func(it *StageTrackerItem) { it.NeedsHuman = true }), ahead: []*StageTrackerItem{headSoaking}, head: headSoaking, opts: opts, + headState: mergeTrainHeadSoaking, reason: "soaking, 12m left"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + stageTrackerMergeTrainQueueFacts(tc.self, tc.ahead, tc.head, tc.opts, nil) + mt := tc.self.MergeTrain + if mt.Length != 3 || mt.HeadNumber != 15315 { + t.Fatalf("length/head = %d/%d", mt.Length, mt.HeadNumber) + } + if mt.HeadState != tc.headState || mt.HeadReason != tc.reason { + t.Fatalf("head = %q %q, want %q %q", mt.HeadState, mt.HeadReason, tc.headState, tc.reason) + } + if mt.EtaS != tc.etaS || mt.EtaBasis != tc.basis { + t.Fatalf("eta_s = %d (%q), want %d (%q)", mt.EtaS, mt.EtaBasis, tc.etaS, tc.basis) + } + if tc.etaS > 0 && (mt.EtaAt == nil || !mt.EtaAt.Equal(now.Add(time.Duration(tc.etaS)*time.Second))) { + t.Fatalf("eta_at = %v, want now+%ds", mt.EtaAt, tc.etaS) + } + if tc.etaS == 0 && mt.EtaAt != nil { + t.Fatalf("no estimate must carry no eta_at: %v", mt.EtaAt) + } + if mt.Stalled != tc.stalled || mt.StalledS != tc.stalledS { + t.Fatalf("stalled = %v/%ds, want %v/%ds", mt.Stalled, mt.StalledS, tc.stalled, tc.stalledS) + } + }) + } +} + +// "Running late": a member still waiting twice past its first estimate at +// the same position reads late; a new position starts a fresh estimate; +// a blocked spell forgets the mark. +func TestStageTrackerMergeTrainEtaLate(t *testing.T) { + now := time.Date(2026, 10, 29, 15, 0, 0, 0, time.UTC) + head := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15315, Stage: trackerNumberAwaitingMerge, MergeGate: &StageTrackerMergeGate{State: MergeGateStateTrainFront}} + self := &StageTrackerItem{Repo: "hivecommons/hive", Kind: "pr", Number: 15319, Stage: trackerNumberAwaitingMerge, + MergeTrain: &StageTrackerMergeTrain{ID: "v5#15315", State: MergeGateStateQueuedBehind, Front: 15315, Position: 2, Size: 2}} + var mem mergeTrainMemory + at := func(d time.Duration) mergeTrainQueueOptions { + return mergeTrainQueueOptions{Now: now.Add(d), Sweep: 5 * time.Minute} + } + run := func(d time.Duration) *StageTrackerMergeTrain { + stageTrackerMergeTrainQueueFacts(self, []*StageTrackerItem{head}, head, at(d), &mem) + return self.MergeTrain + } + first := now.Add(5 * time.Minute) + if mt := run(0); mt.EtaS != 300 || mt.Late || !mt.EtaAt.Equal(first) { + t.Fatalf("first estimate = %+v", mt) + } + // Two polls later the fresh estimate would end 2m later; the anchor + // holds so the countdown does not reset. + if mt := run(2 * time.Minute); !mt.EtaAt.Equal(first) || mt.Late { + t.Fatalf("anchored eta_at = %v, want %v (%+v)", mt.EtaAt, first, mt) + } + if mt := run(9 * time.Minute); mt.Late || !mt.EtaAt.Equal(now.Add(14*time.Minute)) { + t.Fatalf("under 2x the first estimate is not late; a passed anchor re-reads fresh: %+v", mt) + } + if mt := run(11 * time.Minute); !mt.Late || mt.EtaS != 300 { + t.Fatalf("past 2x the first estimate reads late, estimate still shown: %+v", mt) + } + // A materially larger estimate (the head's soak restarted) re-anchors. + head.Soak = &StageTrackerSoak{Until: now.Add(11*time.Minute + 30*time.Second + 30*time.Minute)} + if mt := run(11*time.Minute + 30*time.Second); mt.Late || !mt.EtaAt.Equal(now.Add(11*time.Minute+30*time.Second+30*time.Minute)) { + t.Fatalf("grown estimate re-anchors: %+v", mt) + } + head.Soak = nil + // Moving up resets the mark. + self.MergeTrain.Position = 1 + if mt := run(12 * time.Minute); mt.Late { + t.Fatalf("a new position starts over: %+v", mt) + } + // A blocked spell forgets it; unblocked, the clock starts again. + self.NeedsHuman = true + if mt := run(13 * time.Minute); mt.EtaS != 0 || mt.Late { + t.Fatalf("blocked member: %+v", mt) + } + self.NeedsHuman = false + if mt := run(40 * time.Minute); mt.Late { + t.Fatalf("fresh estimate after a block: %+v", mt) + } +} + +// The stall line reaches the activity log once per hour per train, not +// once per sweep nor once per member. +func TestStageTrackerMergeTrainStallLogHourly(t *testing.T) { + var buf bytes.Buffer + prev := slog.Default() + slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil))) + t.Cleanup(func() { slog.SetDefault(prev) }) + + now := time.Date(2026, 10, 29, 15, 0, 0, 0, time.UTC) + mt := func() *StageTrackerMergeTrain { + return &StageTrackerMergeTrain{ID: "v5#15315", Front: 15315, Size: 3, HeadNumber: 15315, HeadState: mergeTrainHeadNeedsHuman, HeadReason: "waiting for a maintainer", Stalled: true, StalledS: 4 * 3600} + } + items := []StageTrackerItem{ + {Repo: "hivecommons/hive", Kind: "pr", Number: 15319, MergeTrain: mt()}, + {Repo: "hivecommons/hive", Kind: "pr", Number: 15374, MergeTrain: mt()}, + {Repo: "hivecommons/hive", Kind: "pr", Number: 15400}, + } + var log mergeTrainMemory + stageTrackerMergeTrainStallWarnings(&log, items, now) + stageTrackerMergeTrainStallWarnings(&log, items, now.Add(10*time.Minute)) + if n := strings.Count(buf.String(), "merge train stalled"); n != 1 { + t.Fatalf("lines after two sweeps in 10m = %d, want 1:\n%s", n, buf.String()) + } + if !strings.Contains(buf.String(), "head=15315") || !strings.Contains(buf.String(), "stalled_for=4h") || !strings.Contains(buf.String(), "2 PR(s) wait behind it") { + t.Fatalf("line = %s", buf.String()) + } + stageTrackerMergeTrainStallWarnings(&log, items, now.Add(61*time.Minute)) + if n := strings.Count(buf.String(), "merge train stalled"); n != 2 { + t.Fatalf("lines after the hour = %d, want 2", n) + } + items[0].MergeTrain.Stalled, items[1].MergeTrain.Stalled = false, false + stageTrackerMergeTrainStallWarnings(&log, items, now.Add(3*time.Hour)) + if n := strings.Count(buf.String(), "merge train stalled"); n != 2 { + t.Fatalf("a recovered train must not log: %d", n) + } +} + +// TestStageTrackerMergeQueueChipNode: a train member's chip reads its queue +// position and nothing else — the same label whether or not the merge +// sweep is running (the sweep is the spinning glyph and a tooltip line) — +// and the sticky bar's Merge queue popover lists the train in order. +func TestStageTrackerMergeQueueChipNode(t *testing.T) { + html := indexHTML(t) + for _, want := range []string{ + `id="activity-monitor-queue" data-action="hamQueueToggle" data-stop="1" aria-haspopup="dialog" aria-expanded="false" aria-controls="activity-monitor-queue-pop"`, + `<div class="ham-merged-pop ham-queue-pop" id="activity-monitor-queue-pop" role="dialog" aria-label="Merge queue" hidden></div>`, + `if (typeof stageTrackerQueueSync === 'function') stageTrackerQueueSync(d && d.items, Date.now());`, + `.ham-queue-stalled { color: var(--yellow); }`, + } { + if !strings.Contains(html, want) { + t.Errorf("index.html missing merge-queue wiring %q", want) + } + } + runStageTrackerRowActionsScript(t, "function esc(s) { return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/\"/g, '"'); }\nconst assert = require('assert');\n"+ + jsConstLine(t, html, "STAGE_TRACKER_TERMINAL")+"\n"+ + jsConstLine(t, html, "STAGE_TRACKER_PRE_MERGE_SOAK")+"\n"+ + jsConstLine(t, html, "STAGE_TRACKER_MERGE_GATE_WORD")+"\n"+ + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_GLYPH")+"\n"+ + jsConstBlock(t, html, "STAGE_TRACKER_GATE_REASON_WORDS")+"\n"+ + jsConstBlock(t, html, "STAGE_TRACKER_ICONS")+"\n"+ + jsFunc(t, html, "stageTrackerIcon")+"\n"+ + jsFunc(t, html, "stageTrackerAgoText")+"\n"+ + jsFunc(t, html, "stageTrackerSoakLeft")+"\n"+ + jsFunc(t, html, "stageTrackerMergeGateStateOf")+"\n"+ + jsFunc(t, html, "stageTrackerGateReasonText")+"\n"+ + stageTrackerMergeGateInfoJS(t, html)+"\n"+ + jsFunc(t, html, "stageTrackerMergeGateWordHtml")+"\n"+ + jsFunc(t, html, "stageTrackerGateCueHtml")+"\n"+ + jsFunc(t, html, "stageTrackerGateCountdownSync")+"\n"+ + jsFunc(t, html, "stageTrackerQueueTrains")+"\n"+ + jsFunc(t, html, "stageTrackerQueueMemberState")+"\n"+ + jsFunc(t, html, "stageTrackerQueuePopHtml")+"\n"+` +const now = Date.parse('2026-10-29T15:06:00Z'); +const mk = (n, title, mt, gate) => ({ repo: 'hivecommons/hive', number: n, kind: 'pr', title, url: 'https://github.com/hivecommons/hive/pull/' + n, stage: 80, modifiers: [], + merge_gate: Object.assign({ state: mt.state, reason: 'behind #15315 in merge train', last_sweep_at: '2026-10-29T15:05:30Z' }, gate || {}), + merge_train: Object.assign({ id: 'v5#15315', front: 15315, size: 3, length: 3, head_number: 15315, overlap_files: ['fleetreport.go'] }, mt) }); +const headFacts = { head_state: 'needs-human', head_reason: 'waiting for a maintainer (untrusted author)', eta_s: 0 }; +const quiet = mk(15319, 'fleetreport: slog feed', Object.assign({ state: 'queued-behind', position: 2, ahead: [15315] }, headFacts), { sweep_running: false }); +const busy = mk(15319, 'fleetreport: slog feed', Object.assign({ state: 'queued-behind', position: 2, ahead: [15315] }, headFacts), { sweep_running: true }); +const a = stageTrackerMergeGateInfo(quiet, now), b = stageTrackerMergeGateInfo(busy, now); +// Stability: the sweep flag changes neither the word nor the detail. +assert.strictEqual(a.word, 'In merge queue · 2 of 3'); +assert.strictEqual(b.word, a.word, 'sweep_running must not change the chip label'); +assert.strictEqual(a.detail, ''); assert.strictEqual(b.detail, ''); +assert.strictEqual(stageTrackerMergeGateWordHtml(quiet, now), stageTrackerMergeGateWordHtml(busy, now), 'identical chip markup'); +assert.ok(!/queued-behind|merge sweep running/.test(a.word + a.detail + b.word + b.detail), 'no slug, no sweep words in the label'); +// The sweep lives in the glyph and the tooltip. +assert.strictEqual(a.cue, 'waiting'); assert.strictEqual(b.cue, 'running'); +assert.ok(b.tip.includes('Checking now…') && !a.tip.includes('Checking now…'), b.tip); +assert.ok(a.tip.startsWith('Behind #15315 (waiting for a maintainer (untrusted author)). Hive merges one at a time, in order. Head #15315 is waiting for a maintainer (untrusted author); no estimate until it moves.'), a.tip); +assert.ok(a.tip.includes('Last checked'), a.tip); +assert.ok(stageTrackerGateCueHtml(busy, now).includes('cue-spin') && !stageTrackerGateCueHtml(quiet, now).includes('cue-spin')); +// A soaking head: the estimate is the head's soak plus one slot per PR ahead. +const soakHead = mk(15374, 'tracker: queue', { state: 'queued-behind', position: 3, ahead: [15315, 15319], head_state: 'soaking', head_reason: 'soaking, 12m left', eta_s: 25 * 60 }, { sweep_running: true }); +const s = stageTrackerMergeGateInfo(soakHead, now); +assert.strictEqual(s.word, 'In merge queue · 3 of 3'); +assert.ok(s.tip.startsWith('Behind #15315 (soaking, 12m left) and 1 more. Hive merges one at a time, in order. Estimated turn: ~25m. Checking now…'), s.tip); +// Stalled: the chip says so, names the head and how long; the markup wears the stalled class. +const stalled = mk(15374, 'tracker: queue', Object.assign({ state: 'queued-behind', position: 3, ahead: [15315, 15319], stalled: true, stalled_s: 3 * 3600 }, headFacts), { sweep_running: true }); +const st = stageTrackerMergeGateInfo(stalled, now); +assert.strictEqual(st.word, 'Merge queue stalled · head #15315 waiting for a maintainer (untrusted author) 3h'); +assert.ok(stageTrackerMergeGateWordHtml(stalled, now).includes('class="ham-seg-t ham-queue-stalled"')); +assert.strictEqual(stageTrackerMergeGateInfo(mk(15374, 't', Object.assign({ state: 'queued-behind', position: 3, stalled: true, stalled_s: 3 * 3600 }, headFacts), { sweep_running: false }), now).word, st.word, 'stalled label is sweep-stable too'); +// The front row reads its place too. +const head = mk(15315, 'fleetreport: head', Object.assign({ state: 'front', position: 1, ahead: [] }, headFacts)); +assert.strictEqual(stageTrackerMergeGateInfo(head, now).word, 'Next to merge · 1 of 3'); +assert.ok(stageTrackerMergeGateInfo(head, now).tip.startsWith('Front of the merge queue (2 more behind it): waiting for a maintainer (untrusted author).'), stageTrackerMergeGateInfo(head, now).tip); +// hive's own #15406 read the bare slug "front" while the front sat in its +// soak (the mirrored soak clock returned the raw state). A front in soak +// keeps its place word and its countdown (own soak left + next sweep). +const frontSoak = mk(15406, 'in soak at the front', { state: 'front', position: 1, size: 4, length: 4, head_number: 15406, front: 15406, ahead: [], head_state: 'soaking', head_reason: 'soaking, 12m left', eta_s: 760, eta_at: new Date(now + 760 * 1000).toISOString(), eta_basis: 'own soak 12m · next sweep in 40s' }, + { state: 'front', soak_until: new Date(now + 12 * 60 * 1000).toISOString(), soak_started_at: new Date(now - 18 * 60 * 1000).toISOString(), sweep_running: true }); +const fs = stageTrackerMergeGateInfo(frontSoak, now); +assert.strictEqual(fs.word, 'Next to merge · 1 of 4', fs.word); assert.strictEqual(fs.detail, '~13m'); +assert.strictEqual(stageTrackerMergeGateInfo(Object.assign({}, frontSoak, { merge_gate: Object.assign({}, frontSoak.merge_gate, { sweep_running: false }) }), now).word, fs.word); +assert.strictEqual(stageTrackerMergeGateWordHtml(frontSoak, now).replace(/<[^>]+>/g, ''), 'Next to merge · 1 of 4 · ~13m'); +// Countdown to auto-merge: eta_at drives "· ~4m" on the chip; the detail carries +// the clock so the shared tick keeps counting and a re-render lands on the same +// instant (no reset on poll). +const etaAt = new Date(now + 4 * 60 * 1000).toISOString(); +const timed = mk(15319, 'fleetreport: slog feed', { state: 'queued-behind', position: 2, ahead: [15315], head_state: 'soaking', head_reason: 'soaking, 3m left', eta_s: 240, eta_at: etaAt, eta_basis: '1 ahead · head soaking 3m · next sweep in 40s' }, { sweep_running: false }); +const ti = stageTrackerMergeGateInfo(timed, now); +assert.strictEqual(ti.word, 'In merge queue · 2 of 3'); assert.strictEqual(ti.detail, '~4m'); assert.strictEqual(ti.until, now + 4 * 60 * 1000); +assert.ok(ti.tip.includes('Estimated turn: ~4m (1 ahead · head soaking 3m · next sweep in 40s).'), ti.tip); +const chipHtml = stageTrackerMergeGateWordHtml(timed, now); +assert.ok(chipHtml.includes('>In merge queue · 2 of 3 <span class="ham-gate-detail" data-ham-until="' + (now + 240000) + '" data-ham-until-kind="queued-behind">· ~4m</span>'), chipHtml); +assert.strictEqual(chipHtml.replace(/<[^>]+>/g, ''), 'In merge queue · 2 of 3 · ~4m'); +// The same payload 90s later (a poll re-render) reads 2m30s, not 4m again. +assert.strictEqual(stageTrackerMergeGateInfo(timed, now + 90 * 1000).detail, '~3m'); +assert.strictEqual(stageTrackerMergeGateInfo(timed, now + 150 * 1000).detail, '~2m'); +// The shared tick redoes the detail node in place. +const node = { attrs: { 'data-ham-until': String(now + 240000), 'data-ham-until-kind': 'queued-behind' }, textContent: '· ~4m', getAttribute(k) { return this.attrs[k] === undefined ? null : this.attrs[k]; } }; +const el = { querySelectorAll() { return [node]; } }; +stageTrackerGateCountdownSync(el, now + 150 * 1000); assert.strictEqual(node.textContent, '· ~2m'); +stageTrackerGateCountdownSync(el, now + 5 * 60 * 1000); assert.strictEqual(node.textContent, '· any sweep now'); +// Running late: no clock, the words instead; the estimate stays in the hover. +const late = mk(15319, 'fleetreport: slog feed', { state: 'queued-behind', position: 2, ahead: [15315], head_state: 'ready', head_reason: 'approved and green; merges on hive\'s next sweep', eta_s: 300, eta_at: etaAt, eta_basis: '1 ahead', late: true }); +const li = stageTrackerMergeGateInfo(late, now); +assert.strictEqual(li.detail, 'running late'); assert.strictEqual(li.until, 0); +assert.ok(li.tip.includes('Running late — estimated ~5m, still waiting (1 ahead).'), li.tip); +// Blocked (head on a person / CI): no clock, the blocker instead. +assert.strictEqual(stageTrackerMergeGateInfo(quiet, now).detail, ''); +assert.strictEqual(stageTrackerMergeGateInfo(stalled, now).detail, ''); +assert.strictEqual(stageTrackerMergeGateInfo(mk(15319, 't', { state: 'queued-behind', position: 2, head_state: 'ci', head_reason: 'waiting for CI', eta_s: 0 }), now).detail, ''); +// The popover: trains in merge order with #, title, state words and ETA; no slugs. +const other = { repo: 'hivecommons/hive', number: 1, kind: 'pr', stage: 50, title: 'no train' }; +const pop = stageTrackerQueuePopHtml([soakHead, other, head, quiet], now); +assert.ok(stageTrackerQueuePopHtml([timed, head], now).includes('ETA ~4m') && stageTrackerQueuePopHtml([late, head], now).includes('running late')); +assert.ok(pop.includes('<span>Merge queue</span>') && pop.includes('3 PRs in 1 train'), pop); +const order = [...pop.matchAll(/pull\/(\d+)"/g)].map(m => m[1]); +assert.deepStrictEqual(order, ['15315', '15319', '15374'], pop); +assert.ok(pop.includes('1. <a') && pop.includes('>fleetreport: head<') && pop.includes('waiting for a maintainer (untrusted author)'), pop); +assert.ok(pop.includes('3. <a') && pop.includes('ETA ~25m'), pop); +assert.ok(pop.includes('head #15315'), pop); +assert.ok(!/queued-behind|rebase-exhausted/.test(pop), 'popover has no slugs'); +assert.ok(stageTrackerQueuePopHtml([other], now).includes('No merge train right now')); +console.log('merge queue chip ok'); +`) +} + +// mergeTrainStateSlugs is every merge_train / merge_gate train state the +// server can emit; the SPA must word each one (hivecommons/hive#15395 — +// hive's own #15406 once read the bare slug "front"). +var mergeTrainStateSlugs = []string{ + prstage.MergeTrainStateFront, + prstage.MergeTrainStateQueuedBehind, + prstage.MergeTrainStateRebasing, + prstage.MergeTrainStateRebaseExhausted, + prstage.MergeTrainStateDuplicateOf, +} + +// TestStageTrackerMergeTrainStateSlugsNeverLeak renders every train state +// through the chip words (with and without a soak clock and a running +// sweep) and fails if a slug shows up where a person reads. +func TestStageTrackerMergeTrainStateSlugsNeverLeak(t *testing.T) { + html := indexHTML(t) + table := jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_WORD") + for _, slug := range mergeTrainStateSlugs { + if !strings.Contains(table, "'"+slug+"'") && !strings.Contains(table, " "+slug+":") { + t.Errorf("STAGE_TRACKER_TRAIN_STATE_WORD has no words for %q: %s", slug, table) + } + } + slugsJSON, _ := json.Marshal(mergeTrainStateSlugs) + runStageTrackerRowActionsScript(t, "function esc(s) { return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/\"/g, '"'); }\nconst assert = require('assert');\n"+ + jsConstLine(t, html, "STAGE_TRACKER_TERMINAL")+"\n"+ + jsConstLine(t, html, "STAGE_TRACKER_PRE_MERGE_SOAK")+"\n"+ + jsConstLine(t, html, "STAGE_TRACKER_MERGE_GATE_WORD")+"\n"+ + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_GLYPH")+"\n"+ + jsConstBlock(t, html, "STAGE_TRACKER_GATE_REASON_WORDS")+"\n"+ + jsConstBlock(t, html, "STAGE_TRACKER_ICONS")+"\n"+ + jsFunc(t, html, "stageTrackerIcon")+"\n"+ + jsFunc(t, html, "stageTrackerAgoText")+"\n"+ + jsFunc(t, html, "stageTrackerSoakLeft")+"\n"+ + jsFunc(t, html, "stageTrackerMergeGateStateOf")+"\n"+ + jsFunc(t, html, "stageTrackerGateReasonText")+"\n"+ + stageTrackerMergeGateInfoJS(t, html)+"\n"+ + jsFunc(t, html, "stageTrackerMergeGateWordHtml")+"\n"+ + jsFunc(t, html, "stageTrackerGateCueHtml")+"\n"+ + jsFunc(t, html, "stageTrackerQueueMemberState")+"\n"+` +const slugs = `+string(slugsJSON)+`; +const now = Date.parse('2026-10-29T15:06:00Z'); +// A leak is the slug standing alone (the whole label or one " · " segment, +// case-sensitive: slugs are lowercase, "Rebasing · attempt 1/2" is words) +// or a hyphenated slug anywhere. +const leaks = (s, state) => { s = String(s || ''); return s.trim() === state || s.split(/\s*·\s*/).some(seg => seg.trim() === state) || (state.includes('-') && s.includes(state)); }; +const soak = { soak_until: new Date(now + 9 * 60 * 1000).toISOString(), soak_started_at: new Date(now - 60 * 1000).toISOString() }; +for (const state of slugs) { + for (const sweep of [true, false]) for (const clock of [null, soak]) { + // A held duplicate rides no train: the gate alone says "duplicate-of:<n>". + const dup = state === 'duplicate-of'; + const mt = { id: 'v5#15406', state, front: 15406, head_number: 15406, position: state === 'front' ? 1 : 2, size: 4, length: 4, attempt: 1, max_attempts: 2, ahead: state === 'front' ? [] : [15406] }; + const item = { repo: 'hivecommons/hive', number: 15407, kind: 'pr', title: 't', stage: 80, modifiers: [], merge_train: dup ? undefined : mt, + merge_gate: Object.assign({ state: dup ? state + ':15402' : state, sweep_running: sweep, duplicate_of: dup ? 15402 : 0, duplicate_issue: dup ? 15396 : 0 }, clock || {}) }; + const word = dup ? stageTrackerMergeGateStateWord(item.merge_gate.state) : stageTrackerTrainWord(item); + assert.ok(word && !leaks(word, state), state + ' train word: ' + JSON.stringify(word)); + const info = stageTrackerMergeGateInfo(item, now); + assert.ok(info.word && !leaks(info.word, state) && !leaks(info.detail, state), state + ' chip: ' + JSON.stringify(info)); + assert.ok(!leaks(stageTrackerMergeGateWordHtml(item, now).replace(/<[^>]+>/g, ''), state), state + ' html'); + assert.ok(!leaks(stageTrackerQueueMemberState(item, now), state), state + ' popover'); + } +} +// The word table covers the slugs and an unmapped state still spells out. +for (const state of slugs) assert.ok(STAGE_TRACKER_TRAIN_STATE_WORD[state], state); +assert.strictEqual(stageTrackerMergeGateStateWord('front'), 'Next to merge'); +assert.strictEqual(stageTrackerMergeGateStateWord('duplicate-of:15402'), 'Duplicate of #15402'); +// The held duplicate's chip: "Duplicate of #15402 · needs a pick", the hover names the issue and the pick rule. +const heldDup = { repo: 'hivecommons/hive', number: 15407, kind: 'pr', title: 't', stage: 80, modifiers: ['needs-decision'], merge_gate: { state: 'duplicate-of:15402', duplicate_of: 15402, duplicate_issue: 15396, sweep_running: true } }; +const hd = stageTrackerMergeGateInfo(heldDup, now); +assert.strictEqual(hd.word, 'Duplicate of #15402'); assert.strictEqual(hd.detail, 'needs a pick'); assert.strictEqual(hd.until, 0); assert.strictEqual(hd.duplicateOf, 15402); +assert.ok(hd.tip.startsWith('This PR and #15402 both fix #15396. Hive\'s merge queue carries one PR per issue and kept #15402'), hd.tip); +assert.strictEqual(stageTrackerMergeGateWordHtml(heldDup, now).replace(/<[^>]+>/g, ''), 'Duplicate of #15402 · needs a pick'); +heldDup.merge_gate.sweep_running = false; +assert.strictEqual(stageTrackerMergeGateInfo(heldDup, now).word + ' · ' + stageTrackerMergeGateInfo(heldDup, now).detail, 'Duplicate of #15402 · needs a pick', 'sweep-stable'); +assert.strictEqual(stageTrackerMergeGateStateWord('some-new-state'), 'Some new state'); +`) +} diff --git a/src/pkg/dashboard/stage_tracker_merge_train_test.go b/src/pkg/dashboard/stage_tracker_merge_train_test.go index b5686f2f4b..00303b6bb1 100644 --- a/src/pkg/dashboard/stage_tracker_merge_train_test.go +++ b/src/pkg/dashboard/stage_tracker_merge_train_test.go @@ -107,7 +107,7 @@ func TestStageTrackerMergeTrainPayload(t *testing.T) { t.Fatal(err) } for _, want := range []string{`"merge_gate":{"state":"queued-behind","reason":"behind #15074 in merge train (2nd of 4, overlapping files: stage_tracker.go, index.html)"`, - `"merge_train":{"id":"v5#15074","state":"queued-behind","front":15074,"position":2,"size":4,"overlap_files":["stage_tracker.go","index.html"],"ahead":[15074]}`} { + `"merge_train":{"id":"v5#15074","state":"queued-behind","front":15074,"position":2,"size":4,"overlap_files":["stage_tracker.go","index.html"],"ahead":[15074],"length":4,"head_number":15074,"head_state":"ready","head_reason":"approved and green; merges on hive's next sweep","head_since":"2026-10-27T14:00:00Z","eta_s":300,"eta_at":"2026-10-27T14:05:00Z","eta_basis":"1 ahead"}`} { if !strings.Contains(string(raw), want) { t.Fatalf("payload lacks %s:\n%s", want, raw) } @@ -201,6 +201,10 @@ func TestStageTrackerMergeTrainSPA(t *testing.T) { jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_QUEUED"), jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_REBASING"), jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_EXHAUSTED"), + jsConstLine(t, html, "STAGE_TRACKER_TRAIN_STATE_WORD"), + jsConstLine(t, html, "STAGE_TRACKER_GATE_DUPLICATE_PREFIX"), + jsFunc(t, html, "stageTrackerGateDuplicateOf"), + jsFunc(t, html, "stageTrackerMergeGateStateWord"), jsConstLine(t, html, "STAGE_TRACKER_TRAIN_GLYPH"), jsConstBlock(t, html, "STAGE_TRACKER_ICONS"), stageTrackerManualMergeJS(t, html), @@ -228,7 +232,14 @@ func TestStageTrackerMergeTrainSPA(t *testing.T) { jsFunc(t, html, "stageTrackerHeadline"), jsFunc(t, html, "stageTrackerTrain"), jsFunc(t, html, "stageTrackerOrdinal"), + jsFunc(t, html, "stageTrackerShortLeft"), + jsFunc(t, html, "stageTrackerTrainPlace"), + jsFunc(t, html, "stageTrackerTrainStalledWord"), jsFunc(t, html, "stageTrackerTrainWord"), + jsFunc(t, html, "stageTrackerTrainEtaAt"), + jsFunc(t, html, "stageTrackerTrainEtaWords"), + jsFunc(t, html, "stageTrackerTrainEtaDetail"), + jsFunc(t, html, "stageTrackerTrainQueueTip"), jsFunc(t, html, "stageTrackerTrainLines"), jsFunc(t, html, "stageTrackerTrainKey"), jsFunc(t, html, "stageTrackerTrainContiguous"), @@ -251,16 +262,16 @@ const fourth = mk(15097, 85, { state: 'rebase-exhausted', position: 4, attempt: const other = { repo: 'hivecommons/hive', number: 15100, kind: 'pr', title: 'Docs', stage: 80, modifiers: [] }; // Pill wording per state. -assert.strictEqual(stageTrackerTrainWord(front), 'Merging · front of train'); -assert.strictEqual(stageTrackerTrainWord(second), 'In merge train · 2nd of 4'); +assert.strictEqual(stageTrackerTrainWord(front), 'Next to merge · 1 of 4'); +assert.strictEqual(stageTrackerTrainWord(second), 'In merge queue · 2 of 4'); assert.strictEqual(stageTrackerTrainWord(third), 'Rebasing · attempt 1/2'); assert.strictEqual(stageTrackerTrainWord(fourth), 'Needs human decision · rebase failed twice'); assert.strictEqual(stageTrackerTrainWord(other), ''); for (const [n, w] of [[1, '1st'], [2, '2nd'], [3, '3rd'], [4, '4th'], [11, '11th'], [12, '12th'], [13, '13th'], [21, '21st'], [22, '22nd'], [111, '111th']]) assert.strictEqual(stageTrackerOrdinal(n), w, 'ordinal ' + n); // The lit chevron carries the word: 080 for the members, the 085 detour for the exhausted one. const lit = it => stageTrackerSegments(it, stages, now).find(s => s.state === 'active'); -assert.strictEqual(lit(front).short, 'Merging · front of train'); -assert.strictEqual(lit(second).short, 'In merge train · 2nd of 4'); +assert.strictEqual(lit(front).short, 'Next to merge · 1 of 4'); +assert.strictEqual(lit(second).short, 'In merge queue · 2 of 4'); assert.strictEqual(lit(third).short, 'Rebasing · attempt 1/2'); assert.strictEqual(lit(fourth).detour.text, 'Needs human decision · rebase failed twice'); assert.strictEqual(lit(other).short, 'Ready for auto merge'); @@ -278,6 +289,18 @@ assert.ok(!stageTrackerTooltip(other, now).includes('Merge train')); // Contiguity: a sorted list that interleaves the train is regrouped at the // first member's slot, in merge order; other rows keep their order. const sorted = [fourth, other, third, { repo: 'x/y', number: 1, kind: 'issue', stage: 50 }, front, second]; +// Queue facts (#15395): the chip is the position, never the slug; the +// hover sentence names the head, what it waits on and the estimate. +const queued = mk(15319, 80, { state: 'queued-behind', position: 2, ahead: [15315], length: 3, head_number: 15315, head_state: 'soaking', head_reason: 'soaking, 12m left', eta_s: 1500 }); +assert.strictEqual(stageTrackerTrainWord(queued), 'In merge queue · 2 of 3'); +assert.strictEqual(stageTrackerTrainQueueTip(queued.merge_train), 'Behind #15315 (soaking, 12m left). Hive merges one at a time, in order. Estimated turn: ~25m.'); +const thirdQ = mk(15374, 80, { state: 'queued-behind', position: 3, ahead: [15315, 15319], length: 3, head_number: 15315, head_state: 'needs-human', head_reason: 'waiting for a maintainer (untrusted author)', eta_s: 0 }); +assert.strictEqual(stageTrackerTrainWord(thirdQ), 'In merge queue · 3 of 3'); +assert.strictEqual(stageTrackerTrainQueueTip(thirdQ.merge_train), 'Behind #15315 (waiting for a maintainer (untrusted author)) and 1 more. Hive merges one at a time, in order. Head #15315 is waiting for a maintainer (untrusted author); no estimate until it moves.'); +const stalledQ = mk(15374, 80, { state: 'queued-behind', position: 3, ahead: [15315, 15319], length: 3, head_number: 15315, head_state: 'ci', head_reason: 'waiting for CI', stalled: true, stalled_s: 3 * 3600 }); +assert.strictEqual(stageTrackerTrainWord(stalledQ), 'Merge queue stalled · head #15315 waiting for CI 3h'); +assert.ok(stageTrackerTrainQueueTip(stalledQ.merge_train).includes('head #15315 has not moved for 3h'), stageTrackerTrainQueueTip(stalledQ.merge_train)); +for (const it of [queued, thirdQ, stalledQ, front, second]) assert.ok(!/queued-behind|rebase-exhausted/.test(stageTrackerTrainWord(it) + stageTrackerTrainQueueTip(it.merge_train)), 'no slug: ' + it.number); const grouped = stageTrackerTrainContiguous(sorted).map(it => it.number); assert.deepStrictEqual(grouped, [15074, 15085, 15087, 15097, 15100, 1]); // Header line before the run, once; rail classes on members, last closes it. diff --git a/src/pkg/dashboard/static-src/css/tracker.css b/src/pkg/dashboard/static-src/css/tracker.css index 3598aac83b..971a06c5ec 100644 --- a/src/pkg/dashboard/static-src/css/tracker.css +++ b/src/pkg/dashboard/static-src/css/tracker.css @@ -467,6 +467,11 @@ .ham-merged-list .ham-merged-meta { color: var(--text-faint); white-space: nowrap; font-size: var(--fs-xs); } .ham-merged-pop .ham-empty { padding: var(--sp-2) 0; } .ham-merged-pop-foot { margin-top: var(--sp-3); color: var(--text-faint); font-size: var(--fs-xs); } + /* Merge queue chip + popover (hivecommons/hive issue 15395): the train in merge order. */ + .ham-queue-chip .ham-queue-n { margin-left: var(--sp-1); font-variant-numeric: tabular-nums; } + .ham-queue-train { margin: var(--sp-3) 0 var(--sp-2); font-weight: var(--fw-semibold); } + .ham-queue-train:first-of-type { margin-top: 0; } + .ham-queue-stalled { color: var(--yellow); } .merge-banners { position: fixed; right: var(--sp-6); bottom: var(--sp-6); z-index: var(--z-merge-banner); display: flex; flex-direction: column; gap: var(--sp-3); max-width: min(26rem, calc(100vw - 2 * var(--sp-6))); pointer-events: none; } .merge-banner { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: var(--sp-4); align-items: start; padding: var(--sp-4) var(--sp-5); border: var(--line-width) solid var(--line); border-left: 3px solid var(--green); border-radius: var(--r); background: var(--panel-strong); color: var(--text); box-shadow: var(--shadow-raised); font-size: var(--fs-sm); pointer-events: auto; animation: merge-banner-in 220ms ease-out both; } .merge-banner.merge-banner-out { animation: merge-banner-out 180ms ease-in both; } diff --git a/src/pkg/dashboard/static-src/html/tracker.html b/src/pkg/dashboard/static-src/html/tracker.html index 574ee03c48..207377e160 100644 --- a/src/pkg/dashboard/static-src/html/tracker.html +++ b/src/pkg/dashboard/static-src/html/tracker.html @@ -39,6 +39,8 @@ <h2 class="config-title" id="activity-monitor-settings-title">Hive activity moni <span class="ham-sticky-live" id="activity-monitor-merged-live" role="status" aria-live="polite"></span> <button type="button" class="ham-chip ham-merged-count" id="activity-monitor-merged" data-action="hamMergedToggle" data-stop="1" aria-haspopup="dialog" aria-expanded="false" aria-controls="activity-monitor-merged-pop" title="Merged: 0 all time"><svg class="ham-merged-ico" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true"><path d="M8 1.5 13.6 4.75v6.5L8 14.5 2.4 11.25v-6.5z"/></svg>Merged <span class="ham-merged-n">0</span><span class="ham-merged-sub">0 today · 0 this week</span></button> <div class="ham-merged-pop" id="activity-monitor-merged-pop" role="dialog" aria-label="Recent merges" hidden></div> + <button type="button" class="ham-chip ham-queue-chip" id="activity-monitor-queue" data-action="hamQueueToggle" data-stop="1" aria-haspopup="dialog" aria-expanded="false" aria-controls="activity-monitor-queue-pop" title="Merge queue: empty" hidden>⛓ Merge queue <span class="ham-queue-n">0</span></button> + <div class="ham-merged-pop ham-queue-pop" id="activity-monitor-queue-pop" role="dialog" aria-label="Merge queue" hidden></div> </div> <div id="activity-monitor-panel"><div class="ham-empty">Loading the tracker…</div></div> <div class="ham-meta ham-meta-foot" id="activity-monitor-meta-foot"></div> diff --git a/src/pkg/dashboard/static-src/js/tracker-filter.js b/src/pkg/dashboard/static-src/js/tracker-filter.js index c3232a0711..2e4116dc21 100644 --- a/src/pkg/dashboard/static-src/js/tracker-filter.js +++ b/src/pkg/dashboard/static-src/js/tracker-filter.js @@ -864,6 +864,7 @@ "not-merged-yet": {"kind": "reason", "plain": "the merge gate hasn't merged it", "detail": "The hive approved it but the merge gate has not merged it; its lane reason says what still blocks it."}, "not-now": {"kind": "action", "plain": "declined for now; closed as not planned", "detail": "The hive declined the proposal for now and closed it as not planned, with the reason and what evidence would reopen it."}, "operator-action": {"kind": "action", "plain": "told the spoke owner what to do; nothing for an agent here", "detail": "The hive named the spoke owner and the one action they take (for example re-authenticate the provider). The item stays parked, gets no approved-direction, and clears when the report stops recurring."}, + "one-pr-per-issue": {"kind": "rule", "plain": "two PRs fix the same issue; the merge queue keeps one", "detail": "Two open PRs declare the same closing issue. The merge queue carries one PR per issue — a green head first, then the earlier-opened — and the other closes as superseded with credit."}, "owner-only": {"kind": "rule", "plain": "only the hive owner can do this", "detail": "This needs a secret, token or organization permission that only the hive owner holds; the hive cannot do it for you."}, "owner_only": {"kind": "rule", "plain": "only the hive owner can do this", "detail": "This needs a secret, token or organization permission that only the hive owner holds; the hive cannot do it for you."}, "park-label-stuck": {"kind": "reason", "plain": "stage/045-needs-decision is still on the issue; the scanner skips it until that label clears", "detail": "The hive approved the issue and applied approved-direction, but the stage/045-needs-decision park label was never removed, so the scanner still skips the issue. Lift the park (it removes the label through the hive's audited label path) or re-evaluate the issue so the stage engine re-reads it."}, diff --git a/src/pkg/dashboard/static-src/js/tracker-merged.js b/src/pkg/dashboard/static-src/js/tracker-merged.js index aadc916866..119cc6ec1f 100644 --- a/src/pkg/dashboard/static-src/js/tracker-merged.js +++ b/src/pkg/dashboard/static-src/js/tracker-merged.js @@ -68,6 +68,7 @@ const celebrate = fresh.length && typeof stageTrackerCelebrateSetting === 'function' && stageTrackerCelebrateSetting(); if (celebrate) fresh.forEach(k => { _stageTrackerCelebratePending[k] = true; }); if (typeof stageTrackerMergedSync === 'function') stageTrackerMergedSync(d, fresh, Date.now()); + if (typeof stageTrackerQueueSync === 'function') stageTrackerQueueSync(d && d.items, Date.now()); if (!fresh.length) return; fresh.forEach(k => { _stageTrackerZapped[k] = true; }); if (celebrate) { stageTrackerCelebrateMerges(fresh); return; } diff --git a/src/pkg/dashboard/static-src/js/tracker-queue.js b/src/pkg/dashboard/static-src/js/tracker-queue.js new file mode 100644 index 0000000000..0ea78036e2 --- /dev/null +++ b/src/pkg/dashboard/static-src/js/tracker-queue.js @@ -0,0 +1,105 @@ + /* ---- Merge queue popover (hivecommons/hive#15395) ---------------------- + The sticky bar's "Merge queue" chip lists every merge train in the + payload in merge order — #, title, state, ETA — so an operator can + read which PR goes next without hovering each chevron. Same popover + pattern as "Merged" (hamMergedToggle). */ + // Pure: the trains in the payload, each as {key, mt, members[]} with + // members in merge order (head first). + function stageTrackerQueueTrains(items) { + const byKey = {}; + const order = []; + for (const it of (Array.isArray(items) ? items : [])) { + const mt = typeof stageTrackerTrain === 'function' ? stageTrackerTrain(it) : null; + if (!mt) continue; + const k = (it.repo || '') + '|' + (mt.id || ('#' + mt.front)); + if (!byKey[k]) { byKey[k] = { key: k, repo: it.repo || '', mt, members: [] }; order.push(k); } + byKey[k].members.push(it); + } + return order.map(k => { + const t = byKey[k]; + t.members.sort((a, b) => (Number(a.merge_train.position) || 0) - (Number(b.merge_train.position) || 0) || ((a.number || 0) - (b.number || 0))); + // The head's own row carries the freshest head facts. + t.mt = t.members[0].merge_train; + return t; + }); + } + // Pure: one member's state in plain words — never the state slug. + function stageTrackerQueueMemberState(it, nowMs) { + const mt = it.merge_train || {}; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT || (Number(mt.position) || 0) === 1) return String(mt.head_reason || '').trim() || 'front of the queue'; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_REBASING) return 'being rebased · attempt ' + (Number(mt.attempt) || 0) + '/' + (Number(mt.max_attempts) || 0); + if (mt.state === STAGE_TRACKER_TRAIN_STATE_EXHAUSTED) return 'needs a human rebase'; + const mg = it.merge_gate || {}; + return 'waiting its turn' + (mg.sweep_running ? ' · checking now' : ''); + } + // Pure: the popover body. + function stageTrackerQueuePopHtml(items, nowMs) { + nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); + const trains = stageTrackerQueueTrains(items); + const head = `<div class="ham-merged-pop-head"><span>Merge queue</span><span class="ham-merged-pop-sum">${trains.length ? esc(String(trains.reduce((n, t) => n + t.members.length, 0))) + ' PRs in ' + esc(String(trains.length)) + (trains.length === 1 ? ' train' : ' trains') : 'no train right now'}</span></div>`; + if (!trains.length) return head + '<div class="ham-empty">No merge train right now. PRs that touch the same files queue here and merge one at a time, in order.</div>'; + const body = trains.map(t => { + const place = stageTrackerTrainPlace(t.mt); + const stalled = stageTrackerTrainStalledWord(t.mt); + const base = String(t.mt.id || '').split('#')[0] || 'base'; + const title = `<div class="ham-queue-train${stalled ? ' ham-queue-stalled' : ''}">${esc(STAGE_TRACKER_TRAIN_GLYPH)} ${esc(String(t.repo || ''))} → ${esc(base)} · ${esc(String(place.len))} PRs · head #${esc(String(place.head))}${stalled ? ' · ' + esc(stalled.replace(/^Merge queue stalled · /, 'stalled: ')) : ''}</div>`; + const rows = t.members.map(it => { + const mt = it.merge_train || {}; + const n = esc(String(it.number || '')); + const num = it.url ? `<a href="${esc(it.url)}" target="_blank" rel="noopener">#${n}</a>` : `#${n}`; + const etaAt = !mt.stalled ? stageTrackerTrainEtaAt(mt, nowMs) : 0; + const eta = etaAt > 0 ? (mt.late ? 'running late' : 'ETA ' + stageTrackerTrainEtaDetail(etaAt, nowMs)) : ''; + const meta = [esc(stageTrackerQueueMemberState(it, nowMs)), esc(eta)].filter(Boolean).join(' · '); + return `<li><span class="ham-merged-num">${esc(String(mt.position || ''))}. ${num}</span><span class="ham-merged-title" title="${esc(it.title || '')}">${esc(it.title || '')}</span><span class="ham-merged-meta">${meta}</span></li>`; + }).join(''); + return title + `<ol class="ham-merged-list ham-queue-list">${rows}</ol>`; + }).join(''); + const foot = '<div class="ham-merged-pop-foot">Hive merges one train member at a time, head first; the rest rebase or update and take the next turn. The ETA is an estimate: the head\u2019s remaining soak, one sweep per PR ahead without a clock, and the wait for the next sweep.</div>'; + return head + body + foot; + } + var _stageTrackerQueueItems = []; + function stageTrackerQueueChip() { return document.getElementById('activity-monitor-queue'); } + // Pure over the DOM: the chip reads the train count and hides with no + // train; an open popover is redrawn. + function stageTrackerQueueSync(items, nowMs) { + _stageTrackerQueueItems = Array.isArray(items) ? items : []; + const chip = stageTrackerQueueChip(); + if (!chip) return; + const trains = stageTrackerQueueTrains(_stageTrackerQueueItems); + const n = trains.reduce((a, t) => a + t.members.length, 0); + const stalled = trains.some(t => t.mt && t.mt.stalled); + chip.hidden = !trains.length; + const nEl = chip.querySelector('.ham-queue-n'); + if (nEl && nEl.textContent !== String(n)) nEl.textContent = String(n); + const title = trains.length ? 'Merge queue: ' + n + ' PRs in ' + trains.length + (trains.length === 1 ? ' train' : ' trains') + (stalled ? ' · stalled' : '') : 'Merge queue: empty'; + if (chip.title !== title) chip.title = title; + chip.classList.toggle('ham-queue-stalled', stalled); + stageTrackerQueuePopDraw(nowMs); + } + function stageTrackerQueuePopDraw(nowMs) { + const pop = document.getElementById('activity-monitor-queue-pop'); + if (!pop || pop.hidden) return; + const html = stageTrackerQueuePopHtml(_stageTrackerQueueItems, nowMs || Date.now()); + if (pop.innerHTML !== html) pop.innerHTML = html; + } + function hamQueueToggle(open) { + const pop = document.getElementById('activity-monitor-queue-pop'); + const chip = stageTrackerQueueChip(); + if (!pop) return false; + const want = typeof open === 'boolean' ? open : !!pop.hidden; + pop.hidden = !want; + if (chip) chip.setAttribute('aria-expanded', want ? 'true' : 'false'); + if (want) stageTrackerQueuePopDraw(Date.now()); + return want; + } + document.addEventListener('click', function(e) { + const pop = document.getElementById('activity-monitor-queue-pop'); + if (!pop || pop.hidden) return; + if (e.target && typeof e.target.closest === 'function' && e.target.closest('#activity-monitor-sticky')) return; + hamQueueToggle(false); + }); + document.addEventListener('keydown', function(e) { + if (e.key !== 'Escape') return; + const pop = document.getElementById('activity-monitor-queue-pop'); + if (pop && !pop.hidden) { hamQueueToggle(false); const chip = stageTrackerQueueChip(); if (chip && chip.focus) chip.focus(); } + }); diff --git a/src/pkg/dashboard/static-src/js/tracker-words.js b/src/pkg/dashboard/static-src/js/tracker-words.js index 031910efda..9fb99d7caf 100644 --- a/src/pkg/dashboard/static-src/js/tracker-words.js +++ b/src/pkg/dashboard/static-src/js/tracker-words.js @@ -138,6 +138,32 @@ const STAGE_TRACKER_TRAIN_STATE_REBASING = 'rebasing'; const STAGE_TRACKER_TRAIN_STATE_EXHAUSTED = 'rebase-exhausted'; const STAGE_TRACKER_TRAIN_GLYPH = '⛓'; + // Every merge_train / merge_gate train state in words. A slug reaches a + // chip only through this table (or stageTrackerTrainWord), never raw: + // hive's own #15406 once read "front" (hivecommons/hive#15395). + const STAGE_TRACKER_TRAIN_STATE_WORD = { front: 'Next to merge', 'queued-behind': 'In merge queue', rebasing: 'Rebasing', 'rebase-exhausted': 'Needs human decision', 'duplicate-of': 'Duplicate of' }; + // merge_gate.state of a PR the queue holds because another open PR + // fixes the same issue: "duplicate-of:15402" (merge_gate.duplicate_of + // / duplicate_issue carry the numbers). + const STAGE_TRACKER_GATE_DUPLICATE_PREFIX = 'duplicate-of:'; + // Pure: the PR number a "duplicate-of:<n>" gate state yields to; 0 for + // any other state. + function stageTrackerGateDuplicateOf(state) { + state = String(state || ''); + return state.indexOf(STAGE_TRACKER_GATE_DUPLICATE_PREFIX) === 0 ? Number(state.slice(STAGE_TRACKER_GATE_DUPLICATE_PREFIX.length)) || 0 : 0; + } + // Pure: the pill word for any merge_gate.state — the gate table, then + // the train table, then the slug spelled out ("merge-failed" → "Merge + // failed") so an unmapped state still never shows as a slug. + function stageTrackerMergeGateStateWord(state) { + state = String(state || ''); + if (STAGE_TRACKER_MERGE_GATE_WORD[state]) return STAGE_TRACKER_MERGE_GATE_WORD[state]; + const dup = stageTrackerGateDuplicateOf(state); + if (dup) return STAGE_TRACKER_TRAIN_STATE_WORD['duplicate-of'] + ' #' + dup; + if (STAGE_TRACKER_TRAIN_STATE_WORD[state]) return STAGE_TRACKER_TRAIN_STATE_WORD[state]; + const plain = state.replace(/[-_]+/g, ' ').trim(); + return plain ? plain.charAt(0).toUpperCase() + plain.slice(1) : ''; + } function stageTrackerTrain(item) { const mt = item && item.merge_train; return mt && typeof mt === 'object' && mt.state ? mt : null; @@ -148,17 +174,82 @@ if (r100 >= 11 && r100 <= 13) return n + 'th'; return n + (r10 === 1 ? 'st' : r10 === 2 ? 'nd' : r10 === 3 ? 'rd' : 'th'); } + // Pure: the train's head number / queue length / this row's position, + // read from the queue facts (#15395) with the older front/size as the + // fallback. + function stageTrackerTrainPlace(mt) { + return { head: Number(mt.head_number || mt.front) || 0, len: Number(mt.length || mt.size) || 0, pos: Number(mt.position) || 0 }; + } + // Pure: "Merge queue stalled · head #15253 waiting for CI 3h" for a + // train whose head has sat past governor.merge_train_stall_warn_s; '' + // otherwise. The server decides stalled; the words are the head's + // plain-word reason plus how long. + function stageTrackerTrainStalledWord(mt) { + if (!mt || !mt.stalled) return ''; + const p = stageTrackerTrainPlace(mt); + const why = String(mt.head_reason || '').replace(/[.;].*$/, '').trim(); + const dur = stageTrackerShortLeft((Number(mt.stalled_s) || 0) * 1000); + return 'Merge queue stalled · head #' + p.head + (why ? ' ' + why : '') + (dur ? ' ' + dur : ''); + } // The chevron word for a train member ('' for a row not in a train). + // The queued word is the position only — "In merge queue · 2 of 6" — + // so it reads the same with the merge sweep on or off (#15395); never + // the state slug. function stageTrackerTrainWord(item) { const mt = stageTrackerTrain(item); if (!mt) return ''; + const stalled = stageTrackerTrainStalledWord(mt); + if (stalled && (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT || mt.state === STAGE_TRACKER_TRAIN_STATE_QUEUED)) return stalled; + const p = stageTrackerTrainPlace(mt); switch (mt.state) { - case STAGE_TRACKER_TRAIN_STATE_FRONT: return 'Merging · front of train'; - case STAGE_TRACKER_TRAIN_STATE_QUEUED: return 'In merge train · ' + stageTrackerOrdinal(mt.position) + ' of ' + (Number(mt.size) || 0); + case STAGE_TRACKER_TRAIN_STATE_FRONT: return STAGE_TRACKER_TRAIN_STATE_WORD[mt.state] + ' · 1 of ' + p.len; + case STAGE_TRACKER_TRAIN_STATE_QUEUED: return STAGE_TRACKER_TRAIN_STATE_WORD[mt.state] + ' · ' + p.pos + ' of ' + p.len; case STAGE_TRACKER_TRAIN_STATE_REBASING: return 'Rebasing · attempt ' + (Number(mt.attempt) || 0) + '/' + (Number(mt.max_attempts) || 0); case STAGE_TRACKER_TRAIN_STATE_EXHAUSTED: return 'Needs human decision · rebase failed ' + (Number(mt.attempt) === 2 ? 'twice' : (Number(mt.attempt) || 0) + ' times'); } - return ''; + return STAGE_TRACKER_TRAIN_STATE_WORD[mt.state] || stageTrackerMergeGateStateWord(mt.state); + } + // Pure: the queued row's hover sentence — "Behind #15253 (soaking, 12m + // left) and 1 more. Hive merges one at a time, in order. Estimated + // turn: ~25m." A head on a person or CI has no estimate, so the + // sentence names what the head waits on instead. + // Pure: the member's countdown target in ms (eta_at, else now+eta_s); + // 0 with no estimate. + function stageTrackerTrainEtaAt(mt, nowMs) { + if (!mt) return 0; + const at = mt.eta_at ? Date.parse(mt.eta_at) : 0; + if (at > 0) return at; + const s = Number(mt.eta_s) || 0; + return s > 0 ? nowMs + s * 1000 : 0; + } + // Pure: the "Estimated turn" sentence — "~25m (2 ahead · next sweep in + // 40s)", "running late — estimated ~25m, still waiting", or '' with no + // estimate. Always a "~": an estimate, not a promise. + function stageTrackerTrainEtaWords(mt, nowMs) { + const at = stageTrackerTrainEtaAt(mt, nowMs); + if (!(at > 0)) return ''; + const left = stageTrackerShortLeft(at - nowMs); + const basis = String(mt.eta_basis || '').trim(); + if (mt.late) return 'Running late — estimated ~' + stageTrackerShortLeft((Number(mt.eta_s) || 0) * 1000) + ', still waiting' + (basis ? ' (' + basis + ')' : '') + '.'; + return 'Estimated turn: ' + (left ? '~' + left : 'any sweep now') + (basis ? ' (' + basis + ')' : '') + '.'; + } + function stageTrackerTrainQueueTip(mt, nowMs) { + if (!mt) return ''; + nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); + const p = stageTrackerTrainPlace(mt); + const why = String(mt.head_reason || '').trim(); + const etaWords = stageTrackerTrainEtaWords(mt, nowMs); + const stalledFor = stageTrackerShortLeft((Number(mt.stalled_s) || 0) * 1000); + const order = 'Hive merges one at a time, in order.'; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT) { + return 'Front of the merge queue' + (p.len > 1 ? ' (' + (p.len - 1) + ' more behind it)' : '') + (why ? ': ' + why : '') + '. ' + order + (mt.stalled ? ' Stalled: the head has not moved for ' + stalledFor + '.' : (etaWords ? ' ' + etaWords : '')); + } + const more = Math.max(0, p.pos - 2); + let tip = 'Behind #' + p.head + (why ? ' (' + why + ')' : '') + (more ? ' and ' + more + ' more' : '') + '. ' + order; + if (mt.stalled) tip += ' Merge queue stalled: head #' + p.head + ' has not moved for ' + stalledFor + (why ? ' — ' + why : '') + '. No estimate until the head moves.'; + else if (etaWords) tip += ' ' + etaWords; + else if (why) tip += ' Head #' + p.head + ' is ' + why + '; no estimate until it moves.'; + return tip; } // Tooltip lines: the queue ahead and the overlapping files. function stageTrackerTrainLines(item) { @@ -596,10 +687,41 @@ if (dec) return dec; const laneReason = item && item.kind !== 'issue' && !stageTrackerCoverReasonText(item) ? item.reason : ''; nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); - const word = STAGE_TRACKER_MERGE_GATE_WORD[state] || state; + const word = stageTrackerMergeGateStateWord(state); const nextAt = mg.next_sweep_at ? Date.parse(mg.next_sweep_at) : 0; const lastAt = mg.last_sweep_at ? Date.parse(mg.last_sweep_at) : 0; const lastSeen = lastAt > 0 ? ' Last checked ' + stageTrackerAgoText(lastAt, nowMs) + '.' : ''; + // Two open PRs for one issue (hivecommons/hive#15395): the queue + // kept one and holds this one — "Duplicate of #15402 · needs a + // pick"; no clock, no train. Let hive decide on either row closes + // the held one as superseded. + const dupOf = stageTrackerGateDuplicateOf(state); + if (dupOf) { + const issue = Number(mg.duplicate_issue) || 0; + const tip = 'This PR and #' + dupOf + ' both fix' + (issue ? ' #' + issue : ' the same issue') + ". Hive's merge queue carries one PR per issue and kept #" + dupOf + ' (a green head first, then the earlier-opened); this one is held out of the queue. Let hive decide on either row closes this one as superseded with credit, or close it yourself.' + lastSeen; + return { state, word: word, detail: 'needs a pick', tip, cue: 'waiting', until: 0, duplicateOf: dupOf }; + } + // A merge-train member (hivecommons/hive#15395): the label is its + // place in the queue and nothing else, so the chip does not flip + // between "… · merge sweep running" and the bare form each sweep, + // and a front in its soak keeps its place word (never the slug) — + // its countdown already includes the soak left. + // The sweep is the spinning ⬡ and a "Checking now…" tooltip line. + const trainSrc = stageTrackerTrain(src) ? src : (stageTrackerTrain(item) ? item : null); + const mt = trainSrc ? stageTrackerTrain(trainSrc) : null; + if (mt && (state === STAGE_TRACKER_TRAIN_STATE_FRONT || state === STAGE_TRACKER_TRAIN_STATE_QUEUED || state === STAGE_TRACKER_TRAIN_STATE_REBASING || state === STAGE_TRACKER_TRAIN_STATE_EXHAUSTED)) { + const trainWord = stageTrackerTrainWord(trainSrc) || 'In merge queue'; + const queued = state === STAGE_TRACKER_TRAIN_STATE_FRONT || state === STAGE_TRACKER_TRAIN_STATE_QUEUED; + const queueTip = queued ? stageTrackerTrainQueueTip(mt, nowMs) : stageTrackerTrainLines(trainSrc).join('. ') + '.'; + const checking = mg.sweep_running ? ' Checking now…' : ''; + // The countdown to its turn ("· ~4m") ticks from eta_at like the + // soak clock, so a poll re-render does not reset it; a blocked + // head or a stalled train shows no clock; past twice the first + // estimate it reads "running late". + const etaAt = queued && !mt.stalled ? stageTrackerTrainEtaAt(mt, nowMs) : 0; + const detail = etaAt > 0 ? (mt.late ? 'running late' : stageTrackerTrainEtaDetail(etaAt, nowMs)) : ''; + return { state, word: trainWord, detail, tip: queueTip + checking + lastSeen, cue: mg.sweep_running ? 'running' : 'waiting', until: etaAt > 0 && !mt.late ? etaAt : 0, train: mt, stalled: !!mt.stalled }; + } // A soak clock (own or mirrored) outranks "merge sweep running": the // sweep reads the clock, it does not replace it. Any gate stage the // lane holds for the soak — 095 admitted, or 060/080 while trust @@ -708,7 +830,7 @@ const c = info.soakClock; const soakAttrs = c ? ` data-ham-soak="1" data-ham-until="${c.until}" data-ham-until-kind="soaking" data-ham-soak-window="${c.windowS}" data-ham-soak-head="${esc(c.head)}" data-ham-soak-restarted="${c.restarted ? 1 : 0}" data-ham-soak-prefix="${esc(c.prefix || '')}" data-ham-next="${c.nextAt || 0}" data-ham-sweeping="${c.sweepRunning ? 1 : 0}" data-ham-soak-tip="${esc(info.soak)}"` : ''; const detail = info.detail ? ` <span class="ham-gate-detail"${soakAttrs || (info.until ? ` data-ham-until="${info.until}" data-ham-until-kind="${esc(info.state)}"` : '')}>· ${esc(info.detail)}</span>` : ''; - return `<span class="ham-seg-t">${esc(info.word)}${detail}</span>`; + return `<span class="ham-seg-t${info.stalled ? ' ham-queue-stalled' : ''}">${esc(info.word)}${detail}</span>`; } /* ---- Soak clock --------------------------------------------------------- The payload's `soak` {started_at, until, window_s, head_sha} is the @@ -782,6 +904,12 @@ const spin = info.cue === 'running' || info.cue === 'merging'; return `<span class="ham-gate-ico${spin ? ' cue-spin' : ''}" data-gate-cue="${esc(info.cue)}" role="img" title="${esc(info.tip)}" aria-label="${esc(info.tip)}">${stageTrackerIcon(spin ? 'sweep' : 'wait', 'ham-ico')}</span>`; } + // Pure: the queue chip's countdown words — "~4m" until the estimate, + // "any sweep now" once it has passed. + function stageTrackerTrainEtaDetail(etaAt, nowMs) { + const left = stageTrackerShortLeft(etaAt - nowMs); + return left ? '~' + left : 'any sweep now'; + } // Pure: ticks every countdown in el from nowMs; "sweep due now" once a // ready row's next pass has come. function stageTrackerGateCountdownSync(el, nowMs) { @@ -794,6 +922,7 @@ const left = stageTrackerShortLeft(until - nowMs); let t = kind === 'soaking' ? (left ? '· ' + left + ' left' : '· soak window complete') : (left ? '· next sweep in ' + left : '· sweep due now'); if (kind === 'decided') t = n.textContent.replace(/ · in [^·]*$|· in [^·]*$/, '') + (left ? ' · in ' + left : ' · due now'); + if (kind === STAGE_TRACKER_TRAIN_STATE_QUEUED || kind === STAGE_TRACKER_TRAIN_STATE_FRONT) t = '· ' + stageTrackerTrainEtaDetail(until, nowMs); if (n.textContent !== t) n.textContent = t; } } diff --git a/src/pkg/dashboard/static-src/manifest.txt b/src/pkg/dashboard/static-src/manifest.txt index 8cbf37ceb1..343b88f9e1 100644 --- a/src/pkg/dashboard/static-src/manifest.txt +++ b/src/pkg/dashboard/static-src/manifest.txt @@ -68,6 +68,7 @@ js/tracker-motion.js js/tracker-filter.js js/tracker-actions.js js/tracker-merged.js +js/tracker-queue.js js/tracker-sound.js js/agent-cards.js js/banners.js diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 53a0086f61..2dee85cc4c 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1790,6 +1790,11 @@ .ham-merged-list .ham-merged-meta { color: var(--text-faint); white-space: nowrap; font-size: var(--fs-xs); } .ham-merged-pop .ham-empty { padding: var(--sp-2) 0; } .ham-merged-pop-foot { margin-top: var(--sp-3); color: var(--text-faint); font-size: var(--fs-xs); } + /* Merge queue chip + popover (hivecommons/hive issue 15395): the train in merge order. */ + .ham-queue-chip .ham-queue-n { margin-left: var(--sp-1); font-variant-numeric: tabular-nums; } + .ham-queue-train { margin: var(--sp-3) 0 var(--sp-2); font-weight: var(--fw-semibold); } + .ham-queue-train:first-of-type { margin-top: 0; } + .ham-queue-stalled { color: var(--yellow); } .merge-banners { position: fixed; right: var(--sp-6); bottom: var(--sp-6); z-index: var(--z-merge-banner); display: flex; flex-direction: column; gap: var(--sp-3); max-width: min(26rem, calc(100vw - 2 * var(--sp-6))); pointer-events: none; } .merge-banner { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: var(--sp-4); align-items: start; padding: var(--sp-4) var(--sp-5); border: var(--line-width) solid var(--line); border-left: 3px solid var(--green); border-radius: var(--r); background: var(--panel-strong); color: var(--text); box-shadow: var(--shadow-raised); font-size: var(--fs-sm); pointer-events: auto; animation: merge-banner-in 220ms ease-out both; } .merge-banner.merge-banner-out { animation: merge-banner-out 180ms ease-in both; } @@ -7463,6 +7468,8 @@ <h2 class="config-title" id="activity-monitor-settings-title">Hive activity moni <span class="ham-sticky-live" id="activity-monitor-merged-live" role="status" aria-live="polite"></span> <button type="button" class="ham-chip ham-merged-count" id="activity-monitor-merged" data-action="hamMergedToggle" data-stop="1" aria-haspopup="dialog" aria-expanded="false" aria-controls="activity-monitor-merged-pop" title="Merged: 0 all time"><svg class="ham-merged-ico" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true"><path d="M8 1.5 13.6 4.75v6.5L8 14.5 2.4 11.25v-6.5z"/></svg>Merged <span class="ham-merged-n">0</span><span class="ham-merged-sub">0 today · 0 this week</span></button> <div class="ham-merged-pop" id="activity-monitor-merged-pop" role="dialog" aria-label="Recent merges" hidden></div> + <button type="button" class="ham-chip ham-queue-chip" id="activity-monitor-queue" data-action="hamQueueToggle" data-stop="1" aria-haspopup="dialog" aria-expanded="false" aria-controls="activity-monitor-queue-pop" title="Merge queue: empty" hidden>⛓ Merge queue <span class="ham-queue-n">0</span></button> + <div class="ham-merged-pop ham-queue-pop" id="activity-monitor-queue-pop" role="dialog" aria-label="Merge queue" hidden></div> </div> <div id="activity-monitor-panel"><div class="ham-empty">Loading the tracker…</div></div> <div class="ham-meta ham-meta-foot" id="activity-monitor-meta-foot"></div> @@ -14292,6 +14299,32 @@ <h3 class="faq-group-title">Where to get help</h3> const STAGE_TRACKER_TRAIN_STATE_REBASING = 'rebasing'; const STAGE_TRACKER_TRAIN_STATE_EXHAUSTED = 'rebase-exhausted'; const STAGE_TRACKER_TRAIN_GLYPH = '⛓'; + // Every merge_train / merge_gate train state in words. A slug reaches a + // chip only through this table (or stageTrackerTrainWord), never raw: + // hive's own #15406 once read "front" (hivecommons/hive#15395). + const STAGE_TRACKER_TRAIN_STATE_WORD = { front: 'Next to merge', 'queued-behind': 'In merge queue', rebasing: 'Rebasing', 'rebase-exhausted': 'Needs human decision', 'duplicate-of': 'Duplicate of' }; + // merge_gate.state of a PR the queue holds because another open PR + // fixes the same issue: "duplicate-of:15402" (merge_gate.duplicate_of + // / duplicate_issue carry the numbers). + const STAGE_TRACKER_GATE_DUPLICATE_PREFIX = 'duplicate-of:'; + // Pure: the PR number a "duplicate-of:<n>" gate state yields to; 0 for + // any other state. + function stageTrackerGateDuplicateOf(state) { + state = String(state || ''); + return state.indexOf(STAGE_TRACKER_GATE_DUPLICATE_PREFIX) === 0 ? Number(state.slice(STAGE_TRACKER_GATE_DUPLICATE_PREFIX.length)) || 0 : 0; + } + // Pure: the pill word for any merge_gate.state — the gate table, then + // the train table, then the slug spelled out ("merge-failed" → "Merge + // failed") so an unmapped state still never shows as a slug. + function stageTrackerMergeGateStateWord(state) { + state = String(state || ''); + if (STAGE_TRACKER_MERGE_GATE_WORD[state]) return STAGE_TRACKER_MERGE_GATE_WORD[state]; + const dup = stageTrackerGateDuplicateOf(state); + if (dup) return STAGE_TRACKER_TRAIN_STATE_WORD['duplicate-of'] + ' #' + dup; + if (STAGE_TRACKER_TRAIN_STATE_WORD[state]) return STAGE_TRACKER_TRAIN_STATE_WORD[state]; + const plain = state.replace(/[-_]+/g, ' ').trim(); + return plain ? plain.charAt(0).toUpperCase() + plain.slice(1) : ''; + } function stageTrackerTrain(item) { const mt = item && item.merge_train; return mt && typeof mt === 'object' && mt.state ? mt : null; @@ -14302,17 +14335,82 @@ <h3 class="faq-group-title">Where to get help</h3> if (r100 >= 11 && r100 <= 13) return n + 'th'; return n + (r10 === 1 ? 'st' : r10 === 2 ? 'nd' : r10 === 3 ? 'rd' : 'th'); } + // Pure: the train's head number / queue length / this row's position, + // read from the queue facts (#15395) with the older front/size as the + // fallback. + function stageTrackerTrainPlace(mt) { + return { head: Number(mt.head_number || mt.front) || 0, len: Number(mt.length || mt.size) || 0, pos: Number(mt.position) || 0 }; + } + // Pure: "Merge queue stalled · head #15253 waiting for CI 3h" for a + // train whose head has sat past governor.merge_train_stall_warn_s; '' + // otherwise. The server decides stalled; the words are the head's + // plain-word reason plus how long. + function stageTrackerTrainStalledWord(mt) { + if (!mt || !mt.stalled) return ''; + const p = stageTrackerTrainPlace(mt); + const why = String(mt.head_reason || '').replace(/[.;].*$/, '').trim(); + const dur = stageTrackerShortLeft((Number(mt.stalled_s) || 0) * 1000); + return 'Merge queue stalled · head #' + p.head + (why ? ' ' + why : '') + (dur ? ' ' + dur : ''); + } // The chevron word for a train member ('' for a row not in a train). + // The queued word is the position only — "In merge queue · 2 of 6" — + // so it reads the same with the merge sweep on or off (#15395); never + // the state slug. function stageTrackerTrainWord(item) { const mt = stageTrackerTrain(item); if (!mt) return ''; + const stalled = stageTrackerTrainStalledWord(mt); + if (stalled && (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT || mt.state === STAGE_TRACKER_TRAIN_STATE_QUEUED)) return stalled; + const p = stageTrackerTrainPlace(mt); switch (mt.state) { - case STAGE_TRACKER_TRAIN_STATE_FRONT: return 'Merging · front of train'; - case STAGE_TRACKER_TRAIN_STATE_QUEUED: return 'In merge train · ' + stageTrackerOrdinal(mt.position) + ' of ' + (Number(mt.size) || 0); + case STAGE_TRACKER_TRAIN_STATE_FRONT: return STAGE_TRACKER_TRAIN_STATE_WORD[mt.state] + ' · 1 of ' + p.len; + case STAGE_TRACKER_TRAIN_STATE_QUEUED: return STAGE_TRACKER_TRAIN_STATE_WORD[mt.state] + ' · ' + p.pos + ' of ' + p.len; case STAGE_TRACKER_TRAIN_STATE_REBASING: return 'Rebasing · attempt ' + (Number(mt.attempt) || 0) + '/' + (Number(mt.max_attempts) || 0); case STAGE_TRACKER_TRAIN_STATE_EXHAUSTED: return 'Needs human decision · rebase failed ' + (Number(mt.attempt) === 2 ? 'twice' : (Number(mt.attempt) || 0) + ' times'); } - return ''; + return STAGE_TRACKER_TRAIN_STATE_WORD[mt.state] || stageTrackerMergeGateStateWord(mt.state); + } + // Pure: the queued row's hover sentence — "Behind #15253 (soaking, 12m + // left) and 1 more. Hive merges one at a time, in order. Estimated + // turn: ~25m." A head on a person or CI has no estimate, so the + // sentence names what the head waits on instead. + // Pure: the member's countdown target in ms (eta_at, else now+eta_s); + // 0 with no estimate. + function stageTrackerTrainEtaAt(mt, nowMs) { + if (!mt) return 0; + const at = mt.eta_at ? Date.parse(mt.eta_at) : 0; + if (at > 0) return at; + const s = Number(mt.eta_s) || 0; + return s > 0 ? nowMs + s * 1000 : 0; + } + // Pure: the "Estimated turn" sentence — "~25m (2 ahead · next sweep in + // 40s)", "running late — estimated ~25m, still waiting", or '' with no + // estimate. Always a "~": an estimate, not a promise. + function stageTrackerTrainEtaWords(mt, nowMs) { + const at = stageTrackerTrainEtaAt(mt, nowMs); + if (!(at > 0)) return ''; + const left = stageTrackerShortLeft(at - nowMs); + const basis = String(mt.eta_basis || '').trim(); + if (mt.late) return 'Running late — estimated ~' + stageTrackerShortLeft((Number(mt.eta_s) || 0) * 1000) + ', still waiting' + (basis ? ' (' + basis + ')' : '') + '.'; + return 'Estimated turn: ' + (left ? '~' + left : 'any sweep now') + (basis ? ' (' + basis + ')' : '') + '.'; + } + function stageTrackerTrainQueueTip(mt, nowMs) { + if (!mt) return ''; + nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); + const p = stageTrackerTrainPlace(mt); + const why = String(mt.head_reason || '').trim(); + const etaWords = stageTrackerTrainEtaWords(mt, nowMs); + const stalledFor = stageTrackerShortLeft((Number(mt.stalled_s) || 0) * 1000); + const order = 'Hive merges one at a time, in order.'; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT) { + return 'Front of the merge queue' + (p.len > 1 ? ' (' + (p.len - 1) + ' more behind it)' : '') + (why ? ': ' + why : '') + '. ' + order + (mt.stalled ? ' Stalled: the head has not moved for ' + stalledFor + '.' : (etaWords ? ' ' + etaWords : '')); + } + const more = Math.max(0, p.pos - 2); + let tip = 'Behind #' + p.head + (why ? ' (' + why + ')' : '') + (more ? ' and ' + more + ' more' : '') + '. ' + order; + if (mt.stalled) tip += ' Merge queue stalled: head #' + p.head + ' has not moved for ' + stalledFor + (why ? ' — ' + why : '') + '. No estimate until the head moves.'; + else if (etaWords) tip += ' ' + etaWords; + else if (why) tip += ' Head #' + p.head + ' is ' + why + '; no estimate until it moves.'; + return tip; } // Tooltip lines: the queue ahead and the overlapping files. function stageTrackerTrainLines(item) { @@ -14750,10 +14848,41 @@ <h3 class="faq-group-title">Where to get help</h3> if (dec) return dec; const laneReason = item && item.kind !== 'issue' && !stageTrackerCoverReasonText(item) ? item.reason : ''; nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); - const word = STAGE_TRACKER_MERGE_GATE_WORD[state] || state; + const word = stageTrackerMergeGateStateWord(state); const nextAt = mg.next_sweep_at ? Date.parse(mg.next_sweep_at) : 0; const lastAt = mg.last_sweep_at ? Date.parse(mg.last_sweep_at) : 0; const lastSeen = lastAt > 0 ? ' Last checked ' + stageTrackerAgoText(lastAt, nowMs) + '.' : ''; + // Two open PRs for one issue (hivecommons/hive#15395): the queue + // kept one and holds this one — "Duplicate of #15402 · needs a + // pick"; no clock, no train. Let hive decide on either row closes + // the held one as superseded. + const dupOf = stageTrackerGateDuplicateOf(state); + if (dupOf) { + const issue = Number(mg.duplicate_issue) || 0; + const tip = 'This PR and #' + dupOf + ' both fix' + (issue ? ' #' + issue : ' the same issue') + ". Hive's merge queue carries one PR per issue and kept #" + dupOf + ' (a green head first, then the earlier-opened); this one is held out of the queue. Let hive decide on either row closes this one as superseded with credit, or close it yourself.' + lastSeen; + return { state, word: word, detail: 'needs a pick', tip, cue: 'waiting', until: 0, duplicateOf: dupOf }; + } + // A merge-train member (hivecommons/hive#15395): the label is its + // place in the queue and nothing else, so the chip does not flip + // between "… · merge sweep running" and the bare form each sweep, + // and a front in its soak keeps its place word (never the slug) — + // its countdown already includes the soak left. + // The sweep is the spinning ⬡ and a "Checking now…" tooltip line. + const trainSrc = stageTrackerTrain(src) ? src : (stageTrackerTrain(item) ? item : null); + const mt = trainSrc ? stageTrackerTrain(trainSrc) : null; + if (mt && (state === STAGE_TRACKER_TRAIN_STATE_FRONT || state === STAGE_TRACKER_TRAIN_STATE_QUEUED || state === STAGE_TRACKER_TRAIN_STATE_REBASING || state === STAGE_TRACKER_TRAIN_STATE_EXHAUSTED)) { + const trainWord = stageTrackerTrainWord(trainSrc) || 'In merge queue'; + const queued = state === STAGE_TRACKER_TRAIN_STATE_FRONT || state === STAGE_TRACKER_TRAIN_STATE_QUEUED; + const queueTip = queued ? stageTrackerTrainQueueTip(mt, nowMs) : stageTrackerTrainLines(trainSrc).join('. ') + '.'; + const checking = mg.sweep_running ? ' Checking now…' : ''; + // The countdown to its turn ("· ~4m") ticks from eta_at like the + // soak clock, so a poll re-render does not reset it; a blocked + // head or a stalled train shows no clock; past twice the first + // estimate it reads "running late". + const etaAt = queued && !mt.stalled ? stageTrackerTrainEtaAt(mt, nowMs) : 0; + const detail = etaAt > 0 ? (mt.late ? 'running late' : stageTrackerTrainEtaDetail(etaAt, nowMs)) : ''; + return { state, word: trainWord, detail, tip: queueTip + checking + lastSeen, cue: mg.sweep_running ? 'running' : 'waiting', until: etaAt > 0 && !mt.late ? etaAt : 0, train: mt, stalled: !!mt.stalled }; + } // A soak clock (own or mirrored) outranks "merge sweep running": the // sweep reads the clock, it does not replace it. Any gate stage the // lane holds for the soak — 095 admitted, or 060/080 while trust @@ -14862,7 +14991,7 @@ <h3 class="faq-group-title">Where to get help</h3> const c = info.soakClock; const soakAttrs = c ? ` data-ham-soak="1" data-ham-until="${c.until}" data-ham-until-kind="soaking" data-ham-soak-window="${c.windowS}" data-ham-soak-head="${esc(c.head)}" data-ham-soak-restarted="${c.restarted ? 1 : 0}" data-ham-soak-prefix="${esc(c.prefix || '')}" data-ham-next="${c.nextAt || 0}" data-ham-sweeping="${c.sweepRunning ? 1 : 0}" data-ham-soak-tip="${esc(info.soak)}"` : ''; const detail = info.detail ? ` <span class="ham-gate-detail"${soakAttrs || (info.until ? ` data-ham-until="${info.until}" data-ham-until-kind="${esc(info.state)}"` : '')}>· ${esc(info.detail)}</span>` : ''; - return `<span class="ham-seg-t">${esc(info.word)}${detail}</span>`; + return `<span class="ham-seg-t${info.stalled ? ' ham-queue-stalled' : ''}">${esc(info.word)}${detail}</span>`; } /* ---- Soak clock --------------------------------------------------------- The payload's `soak` {started_at, until, window_s, head_sha} is the @@ -14936,6 +15065,12 @@ <h3 class="faq-group-title">Where to get help</h3> const spin = info.cue === 'running' || info.cue === 'merging'; return `<span class="ham-gate-ico${spin ? ' cue-spin' : ''}" data-gate-cue="${esc(info.cue)}" role="img" title="${esc(info.tip)}" aria-label="${esc(info.tip)}">${stageTrackerIcon(spin ? 'sweep' : 'wait', 'ham-ico')}</span>`; } + // Pure: the queue chip's countdown words — "~4m" until the estimate, + // "any sweep now" once it has passed. + function stageTrackerTrainEtaDetail(etaAt, nowMs) { + const left = stageTrackerShortLeft(etaAt - nowMs); + return left ? '~' + left : 'any sweep now'; + } // Pure: ticks every countdown in el from nowMs; "sweep due now" once a // ready row's next pass has come. function stageTrackerGateCountdownSync(el, nowMs) { @@ -14948,6 +15083,7 @@ <h3 class="faq-group-title">Where to get help</h3> const left = stageTrackerShortLeft(until - nowMs); let t = kind === 'soaking' ? (left ? '· ' + left + ' left' : '· soak window complete') : (left ? '· next sweep in ' + left : '· sweep due now'); if (kind === 'decided') t = n.textContent.replace(/ · in [^·]*$|· in [^·]*$/, '') + (left ? ' · in ' + left : ' · due now'); + if (kind === STAGE_TRACKER_TRAIN_STATE_QUEUED || kind === STAGE_TRACKER_TRAIN_STATE_FRONT) t = '· ' + stageTrackerTrainEtaDetail(until, nowMs); if (n.textContent !== t) n.textContent = t; } } @@ -18033,6 +18169,7 @@ <h2 class="config-title" id="activity-monitor-labels-title">All labels</h2> "not-merged-yet": {"kind": "reason", "plain": "the merge gate hasn't merged it", "detail": "The hive approved it but the merge gate has not merged it; its lane reason says what still blocks it."}, "not-now": {"kind": "action", "plain": "declined for now; closed as not planned", "detail": "The hive declined the proposal for now and closed it as not planned, with the reason and what evidence would reopen it."}, "operator-action": {"kind": "action", "plain": "told the spoke owner what to do; nothing for an agent here", "detail": "The hive named the spoke owner and the one action they take (for example re-authenticate the provider). The item stays parked, gets no approved-direction, and clears when the report stops recurring."}, + "one-pr-per-issue": {"kind": "rule", "plain": "two PRs fix the same issue; the merge queue keeps one", "detail": "Two open PRs declare the same closing issue. The merge queue carries one PR per issue — a green head first, then the earlier-opened — and the other closes as superseded with credit."}, "owner-only": {"kind": "rule", "plain": "only the hive owner can do this", "detail": "This needs a secret, token or organization permission that only the hive owner holds; the hive cannot do it for you."}, "owner_only": {"kind": "rule", "plain": "only the hive owner can do this", "detail": "This needs a secret, token or organization permission that only the hive owner holds; the hive cannot do it for you."}, "park-label-stuck": {"kind": "reason", "plain": "stage/045-needs-decision is still on the issue; the scanner skips it until that label clears", "detail": "The hive approved the issue and applied approved-direction, but the stage/045-needs-decision park label was never removed, so the scanner still skips the issue. Lift the park (it removes the label through the hive's audited label path) or re-evaluate the issue so the stage engine re-reads it."}, @@ -20096,6 +20233,7 @@ <h2 class="config-title" id="activity-monitor-labels-title">All labels</h2> const celebrate = fresh.length && typeof stageTrackerCelebrateSetting === 'function' && stageTrackerCelebrateSetting(); if (celebrate) fresh.forEach(k => { _stageTrackerCelebratePending[k] = true; }); if (typeof stageTrackerMergedSync === 'function') stageTrackerMergedSync(d, fresh, Date.now()); + if (typeof stageTrackerQueueSync === 'function') stageTrackerQueueSync(d && d.items, Date.now()); if (!fresh.length) return; fresh.forEach(k => { _stageTrackerZapped[k] = true; }); if (celebrate) { stageTrackerCelebrateMerges(fresh); return; } @@ -20844,6 +20982,111 @@ <h2 class="config-title" id="activity-monitor-labels-title">All labels</h2> // only); when a recently_closed list lands, mark those issue keys true // in stageTrackerMergeState and they will zap through the same path. + /* ---- Merge queue popover (hivecommons/hive#15395) ---------------------- + The sticky bar's "Merge queue" chip lists every merge train in the + payload in merge order — #, title, state, ETA — so an operator can + read which PR goes next without hovering each chevron. Same popover + pattern as "Merged" (hamMergedToggle). */ + // Pure: the trains in the payload, each as {key, mt, members[]} with + // members in merge order (head first). + function stageTrackerQueueTrains(items) { + const byKey = {}; + const order = []; + for (const it of (Array.isArray(items) ? items : [])) { + const mt = typeof stageTrackerTrain === 'function' ? stageTrackerTrain(it) : null; + if (!mt) continue; + const k = (it.repo || '') + '|' + (mt.id || ('#' + mt.front)); + if (!byKey[k]) { byKey[k] = { key: k, repo: it.repo || '', mt, members: [] }; order.push(k); } + byKey[k].members.push(it); + } + return order.map(k => { + const t = byKey[k]; + t.members.sort((a, b) => (Number(a.merge_train.position) || 0) - (Number(b.merge_train.position) || 0) || ((a.number || 0) - (b.number || 0))); + // The head's own row carries the freshest head facts. + t.mt = t.members[0].merge_train; + return t; + }); + } + // Pure: one member's state in plain words — never the state slug. + function stageTrackerQueueMemberState(it, nowMs) { + const mt = it.merge_train || {}; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT || (Number(mt.position) || 0) === 1) return String(mt.head_reason || '').trim() || 'front of the queue'; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_REBASING) return 'being rebased · attempt ' + (Number(mt.attempt) || 0) + '/' + (Number(mt.max_attempts) || 0); + if (mt.state === STAGE_TRACKER_TRAIN_STATE_EXHAUSTED) return 'needs a human rebase'; + const mg = it.merge_gate || {}; + return 'waiting its turn' + (mg.sweep_running ? ' · checking now' : ''); + } + // Pure: the popover body. + function stageTrackerQueuePopHtml(items, nowMs) { + nowMs = typeof nowMs === 'number' ? nowMs : Date.now(); + const trains = stageTrackerQueueTrains(items); + const head = `<div class="ham-merged-pop-head"><span>Merge queue</span><span class="ham-merged-pop-sum">${trains.length ? esc(String(trains.reduce((n, t) => n + t.members.length, 0))) + ' PRs in ' + esc(String(trains.length)) + (trains.length === 1 ? ' train' : ' trains') : 'no train right now'}</span></div>`; + if (!trains.length) return head + '<div class="ham-empty">No merge train right now. PRs that touch the same files queue here and merge one at a time, in order.</div>'; + const body = trains.map(t => { + const place = stageTrackerTrainPlace(t.mt); + const stalled = stageTrackerTrainStalledWord(t.mt); + const base = String(t.mt.id || '').split('#')[0] || 'base'; + const title = `<div class="ham-queue-train${stalled ? ' ham-queue-stalled' : ''}">${esc(STAGE_TRACKER_TRAIN_GLYPH)} ${esc(String(t.repo || ''))} → ${esc(base)} · ${esc(String(place.len))} PRs · head #${esc(String(place.head))}${stalled ? ' · ' + esc(stalled.replace(/^Merge queue stalled · /, 'stalled: ')) : ''}</div>`; + const rows = t.members.map(it => { + const mt = it.merge_train || {}; + const n = esc(String(it.number || '')); + const num = it.url ? `<a href="${esc(it.url)}" target="_blank" rel="noopener">#${n}</a>` : `#${n}`; + const etaAt = !mt.stalled ? stageTrackerTrainEtaAt(mt, nowMs) : 0; + const eta = etaAt > 0 ? (mt.late ? 'running late' : 'ETA ' + stageTrackerTrainEtaDetail(etaAt, nowMs)) : ''; + const meta = [esc(stageTrackerQueueMemberState(it, nowMs)), esc(eta)].filter(Boolean).join(' · '); + return `<li><span class="ham-merged-num">${esc(String(mt.position || ''))}. ${num}</span><span class="ham-merged-title" title="${esc(it.title || '')}">${esc(it.title || '')}</span><span class="ham-merged-meta">${meta}</span></li>`; + }).join(''); + return title + `<ol class="ham-merged-list ham-queue-list">${rows}</ol>`; + }).join(''); + const foot = '<div class="ham-merged-pop-foot">Hive merges one train member at a time, head first; the rest rebase or update and take the next turn. The ETA is an estimate: the head\u2019s remaining soak, one sweep per PR ahead without a clock, and the wait for the next sweep.</div>'; + return head + body + foot; + } + var _stageTrackerQueueItems = []; + function stageTrackerQueueChip() { return document.getElementById('activity-monitor-queue'); } + // Pure over the DOM: the chip reads the train count and hides with no + // train; an open popover is redrawn. + function stageTrackerQueueSync(items, nowMs) { + _stageTrackerQueueItems = Array.isArray(items) ? items : []; + const chip = stageTrackerQueueChip(); + if (!chip) return; + const trains = stageTrackerQueueTrains(_stageTrackerQueueItems); + const n = trains.reduce((a, t) => a + t.members.length, 0); + const stalled = trains.some(t => t.mt && t.mt.stalled); + chip.hidden = !trains.length; + const nEl = chip.querySelector('.ham-queue-n'); + if (nEl && nEl.textContent !== String(n)) nEl.textContent = String(n); + const title = trains.length ? 'Merge queue: ' + n + ' PRs in ' + trains.length + (trains.length === 1 ? ' train' : ' trains') + (stalled ? ' · stalled' : '') : 'Merge queue: empty'; + if (chip.title !== title) chip.title = title; + chip.classList.toggle('ham-queue-stalled', stalled); + stageTrackerQueuePopDraw(nowMs); + } + function stageTrackerQueuePopDraw(nowMs) { + const pop = document.getElementById('activity-monitor-queue-pop'); + if (!pop || pop.hidden) return; + const html = stageTrackerQueuePopHtml(_stageTrackerQueueItems, nowMs || Date.now()); + if (pop.innerHTML !== html) pop.innerHTML = html; + } + function hamQueueToggle(open) { + const pop = document.getElementById('activity-monitor-queue-pop'); + const chip = stageTrackerQueueChip(); + if (!pop) return false; + const want = typeof open === 'boolean' ? open : !!pop.hidden; + pop.hidden = !want; + if (chip) chip.setAttribute('aria-expanded', want ? 'true' : 'false'); + if (want) stageTrackerQueuePopDraw(Date.now()); + return want; + } + document.addEventListener('click', function(e) { + const pop = document.getElementById('activity-monitor-queue-pop'); + if (!pop || pop.hidden) return; + if (e.target && typeof e.target.closest === 'function' && e.target.closest('#activity-monitor-sticky')) return; + hamQueueToggle(false); + }); + document.addEventListener('keydown', function(e) { + if (e.key !== 'Escape') return; + const pop = document.getElementById('activity-monitor-queue-pop'); + if (pop && !pop.hidden) { hamQueueToggle(false); const chip = stageTrackerQueueChip(); if (chip && chip.focus) chip.focus(); } + }); // Pure: the master toggle and a visible tab gate every automatic play. function stageTrackerSoundGate() { return !!_stageTrackerSoundOn && !document.hidden; } diff --git a/src/pkg/delegatedecision/decision_glossary.go b/src/pkg/delegatedecision/decision_glossary.go index 761451c0ee..184256112d 100644 --- a/src/pkg/delegatedecision/decision_glossary.go +++ b/src/pkg/delegatedecision/decision_glossary.go @@ -105,6 +105,8 @@ var glossary = []GlossaryEntry{ Detail: "The PR was pushed by a person (author/human or one of this hive's owners), so the hive never adopts it. It told the author by name what to fix (rebase, sign-off, CI) and relayed the reviewer's question."}, {Slug: "owner-only", Kind: GlossaryRule, Plain: "only the hive owner can do this", Detail: "This needs a secret, token or organization permission that only the hive owner holds; the hive cannot do it for you.", Aliases: []string{"owner_only"}}, + {Slug: RuleOnePRPerIssue, Kind: GlossaryRule, Plain: "two PRs fix the same issue; the merge queue keeps one", + Detail: "Two open PRs declare the same closing issue. The merge queue carries one PR per issue — a green head first, then the earlier-opened — and the other closes as superseded with credit."}, {Slug: "prefer-contributor", Kind: GlossaryRule, Plain: "a contributor's PR and a hive PR do the same work", Detail: "A contributor's PR and a hive-authored PR cover the same work; the hive prefers the contributor's when it is mergeable and current."}, {Slug: "rail-hold", Kind: GlossaryRule, Plain: "the item is on hold", diff --git a/src/pkg/delegatedecision/policy.go b/src/pkg/delegatedecision/policy.go index eee14161b7..cd188f8fa3 100644 --- a/src/pkg/delegatedecision/policy.go +++ b/src/pkg/delegatedecision/policy.go @@ -158,6 +158,10 @@ type CompetingPR struct { HiveAuthored bool Mergeable bool Current bool + // HeldDuplicate: the merge queue holds this competitor out of the + // train as a duplicate of the item (same `Fixes #N`, + // hivecommons/hive#15395); the item is the PR the queue kept. + HeldDuplicate bool } // Input is everything the delegate reads about one parked item. @@ -205,6 +209,11 @@ type Input struct { Overlaps []int // Competing lists the other open PRs for the same work. Competing []CompetingPR + // TrainDuplicateOf is the open PR the merge queue kept for the same + // issue when it held this one as a duplicate (0 = not held); + // TrainDuplicateIssue is that issue (hivecommons/hive#15395). + TrainDuplicateOf int + TrainDuplicateIssue int // ReporterTrustHold: the park is the reporter-trust gate on the fixing PR. ReporterTrustHold bool // AdoptionStalled: a contributor PR whose adoption parked at 085. @@ -571,6 +580,12 @@ func decidePR(in Input) Decision { Actions: []Action{decisionComment(text), {Kind: ActionApproveReview}}, } } + // Two open PRs for one issue (hivecommons/hive#15395): the merge + // queue already picked the survivor (green first, then earlier); the + // held one closes as superseded, the kept one is released. + if d, ok := decideTrainDuplicate(in, ref); ok { + return d + } // A head parked on DCO or the reporter-trust gate must clear that gate // before a competing-PR rule can release or supersede it. if !in.DCOMissing && !in.ReporterTrustHold { @@ -631,6 +646,55 @@ func decidePR(in Input) Decision { return releasePR("default-release", "approve "+ref+": green head, reviewer question answered with the reversible default — proceed as implemented", "no owner rule holds this back; a merge is reversible and keeps work moving") } +// RuleOnePRPerIssue: the merge queue carries one PR per issue; the one it +// held as a duplicate closes as superseded by the one it kept. +const RuleOnePRPerIssue = "one-pr-per-issue" + +// decideTrainDuplicate resolves a same-issue pair the merge queue split +// (hivecommons/hive#15395). The queue's pick stands: a green head beats a +// red one, the earlier-opened wins among equals. Deciding on the held PR +// closes it as superseded by the kept one, with a comment; deciding on +// the kept PR releases it and names the duplicate the queue holds. A +// paused repo only gets the recommendation. +func decideTrainDuplicate(in Input, ref string) (Decision, bool) { + if in.Kind != KindPR { + return Decision{}, false + } + issue := "" + if in.TrainDuplicateIssue > 0 { + issue = fmt.Sprintf(" for issue #%d", in.TrainDuplicateIssue) + } + if in.TrainDuplicateOf > 0 { + winner := fmt.Sprintf("#%d", in.TrainDuplicateOf) + text := fmt.Sprintf("superseded by %s: both PRs fix the same issue%s; the merge queue carries one PR per issue and kept %s (green first, then the earlier-opened)", winner, issue, winner) + rationale := fmt.Sprintf("%s and %s declare the same closing issue%s; the merge queue holds one PR per issue and kept %s, so %s closes as superseded with credit", ref, winner, issue, winner, ref) + if in.RepoPaused { + return recommendOnly(RuleOnePRPerIssue, rationale+"; the repo is paused, so the hive only recommends", "close "+ref+" as superseded by "+winner), true + } + note := fmt.Sprintf("Thank you for this PR. %s fixes the same issue%s and the merge queue carries one PR per issue; it kept %s (green first, then the earlier-opened), so the hive is closing this one as superseded. Your work is credited on the issue.", winner, issue, winner) + return Decision{ + Verdict: VerdictCloseSuperseded, Rule: RuleOnePRPerIssue, Text: text, + Rationale: rationale, + Actions: []Action{{Kind: ActionCloseDuplicate, Target: in.TrainDuplicateOf, Text: note}}, + }, true + } + for _, other := range in.Competing { + if !other.HeldDuplicate { + continue + } + held := fmt.Sprintf("#%d", other.Number) + if in.Conflicting || !in.CIGreen || in.DCOMissing { + return recommendOnly(RuleOnePRPerIssue, + fmt.Sprintf("the merge queue kept %s over %s%s, but %s is not mergeable now (%s); the queue re-picks on its next pass", ref, held, issue, ref, mergeBlockers(in)), + "fix "+ref+" ("+mergeBlockers(in)+"), or decide on "+held+" to let it supersede this one"), true + } + return releasePR(RuleOnePRPerIssue, + fmt.Sprintf("approve %s: the merge queue carries one PR per issue%s and kept this one; %s is held as its duplicate and closes as superseded once this merges", ref, issue, held), + "one PR per issue in the merge queue; the kept PR merges, the held duplicate closes as superseded"), true + } + return Decision{}, false +} + // decideCompeting applies "prefer an adequate contributor PR over a hive // duplicate" when the item has a competing PR. Mergeability alone cannot // establish adequacy, so closing a hive PR requires a human comparison. diff --git a/src/pkg/delegatedecision/policy_train_duplicate_test.go b/src/pkg/delegatedecision/policy_train_duplicate_test.go new file mode 100644 index 0000000000..46bf8f0ff8 --- /dev/null +++ b/src/pkg/delegatedecision/policy_train_duplicate_test.go @@ -0,0 +1,77 @@ +package delegatedecision + +import ( + "strings" + "testing" +) + +// Two open PRs for one issue (hivecommons/hive#15395): the merge queue's +// pick stands. Deciding on the held PR closes it as superseded by the +// kept one; deciding on the kept PR releases it (or, if it has gone red, +// only recommends and names the fix). +func TestDecideTrainDuplicate(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + name string + in Input + verdict Verdict + target int + textHas string + }{ + { + name: "held later PR closes as superseded by the earlier green one", + in: Input{Kind: KindPR, Number: 15407, Title: "fix: x", CIGreen: true, TrainDuplicateOf: 15402, TrainDuplicateIssue: 15396}, + verdict: VerdictCloseSuperseded, target: 15402, textHas: "superseded by #15402: both PRs fix the same issue for issue #15396", + }, + { + name: "held earlier (red) PR closes as superseded by the later green one", + in: Input{Kind: KindPR, Number: 15402, Title: "fix: x", CIGreen: false, TrainDuplicateOf: 15407, TrainDuplicateIssue: 15396}, + verdict: VerdictCloseSuperseded, target: 15407, textHas: "superseded by #15407", + }, + { + name: "held PR on a paused repo: recommend only", + in: Input{Kind: KindPR, Number: 15407, Title: "fix: x", CIGreen: true, RepoPaused: true, TrainDuplicateOf: 15402}, + verdict: VerdictRecommendOnly, textHas: "close #15407 as superseded by #15402", + }, + { + name: "kept green PR is released and names the held duplicate", + in: Input{Kind: KindPR, Number: 15402, Title: "fix: x", CIGreen: true, HiveAuthored: true, TrustedAuthor: true, ContributorOnline: true, Competing: []CompetingPR{{Number: 15407, HeldDuplicate: true, Mergeable: true, Current: true}}}, + verdict: VerdictApprove, textHas: "#15407 is held as its duplicate", + }, + { + name: "kept PR gone red: recommend the fix, or deciding on the held one", + in: Input{Kind: KindPR, Number: 15402, Title: "fix: x", CIGreen: false, HiveAuthored: true, ContributorOnline: true, Competing: []CompetingPR{{Number: 15407, HeldDuplicate: true}}}, + verdict: VerdictRecommendOnly, textHas: "decide on #15407 to let it supersede this one", + }, + { + name: "a plain competitor is not a queue duplicate", + in: Input{Kind: KindPR, Number: 15402, Title: "fix: x", CIGreen: true, HiveAuthored: true, TrustedAuthor: true, ContributorOnline: true, Competing: []CompetingPR{{Number: 15407, HiveAuthored: true, Mergeable: true}}}, + verdict: VerdictApprove, + }, + } { + t.Run(tc.name, func(t *testing.T) { + d := Decide(tc.in) + if d.Verdict != tc.verdict { + t.Fatalf("verdict = %s (%s: %s), want %s", d.Verdict, d.Rule, d.Rationale, tc.verdict) + } + if tc.name != "a plain competitor is not a queue duplicate" && d.Rule != RuleOnePRPerIssue { + t.Fatalf("rule = %s, want %s", d.Rule, RuleOnePRPerIssue) + } + if tc.target > 0 { + var closed []Action + for _, a := range d.Actions { + if a.Kind == ActionCloseDuplicate { + closed = append(closed, a) + } + } + if len(closed) != 1 || closed[0].Target != tc.target || !strings.Contains(closed[0].Text, "credited on the issue") { + t.Fatalf("close action = %+v, want target #%d with credit", closed, tc.target) + } + } + words := d.Text + "\n" + d.Rationale + if tc.textHas != "" && !strings.Contains(words, tc.textHas) { + t.Fatalf("words %q lack %q", words, tc.textHas) + } + }) + } +} diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 13a2f5319f..c1ccb5bdb2 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -432,6 +432,11 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe stage := func(reason string, merged bool, err error) { c.recordStage(stageLaneContributor, displayRepo, owner, repoName, pr, reason, merged, err) } + if _, held := train.duplicateOf(number); held { + result.Skipped++ + stage(c.mergeTrainDuplicateSkip(train, number), false, nil) + continue + } if train.isSibling(number) { result.Skipped++ stage(c.mergeTrainSkip(train, number), false, nil) diff --git a/src/pkg/github/automerge/automerge_forgestate.go b/src/pkg/github/automerge/automerge_forgestate.go index 88a6ea01d6..d217727fa3 100644 --- a/src/pkg/github/automerge/automerge_forgestate.go +++ b/src/pkg/github/automerge/automerge_forgestate.go @@ -354,14 +354,14 @@ func (c *Engine) runEventDrivenSweep(ctx context.Context, maxMerges int, acmmAll } floor := time.NewTicker(c.floorInterval()) defer floor.Stop() - c.sweepClock.armFloor(c.now().Add(c.floorInterval())) + c.sweepClock.armFloorEvery(c.now(), c.floorInterval()) c.sweepPass(ctx, maxMerges, acmmAllowed, nil, "startup") for { select { case <-ctx.Done(): return case <-floor.C: - c.sweepClock.armFloor(c.now().Add(c.floorInterval())) + c.sweepClock.armFloorEvery(c.now(), c.floorInterval()) // The floor pass is low-priority work like the legacy tick: under // rate-limit caution it yields to the queue (#13382); event passes // are targeted and still run. diff --git a/src/pkg/github/automerge/automerge_merge_train.go b/src/pkg/github/automerge/automerge_merge_train.go index 359bb75aef..e084fd5303 100644 --- a/src/pkg/github/automerge/automerge_merge_train.go +++ b/src/pkg/github/automerge/automerge_merge_train.go @@ -298,6 +298,9 @@ type mergeTrainPlan struct { lane, displayRepo, owner, repo string groups []mergeTrainGroup byNumber map[int]*mergeTrainGroup + // duplicates are the PRs held out of every train this pass because + // another open PR fixes the same issue (one PR per issue). + duplicates map[int]mergeTrainDuplicateHold } // group returns the train number rides in, if any. @@ -350,6 +353,7 @@ func (c *Engine) planMergeTrains(ctx context.Context, lane, displayRepo, owner, if !c.mergeTrainPolicyNow().Enabled || c == nil || c.gh == nil { return plan } + prs = c.planDuplicateHolds(plan, owner, repo, displayRepo, prs, prefilter) perBase := map[string][]*gh.PullRequest{} for _, pr := range prs { if pr == nil || pr.GetNumber() <= 0 || pr.GetHead() == nil || pr.GetHead().GetSHA() == "" { diff --git a/src/pkg/github/automerge/automerge_merge_train_duplicate.go b/src/pkg/github/automerge/automerge_merge_train_duplicate.go new file mode 100644 index 0000000000..1d7a2decb7 --- /dev/null +++ b/src/pkg/github/automerge/automerge_merge_train_duplicate.go @@ -0,0 +1,170 @@ +package automerge + +import ( + "sort" + "strconv" + "time" + + gh "github.com/google/go-github/v72/github" + + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stageengine/prstage" +) + +// autoMergeReasonMergeTrainDuplicate is the tick-stat bucket for a PR held +// out of the train because another open PR fixes the same issue. +const autoMergeReasonMergeTrainDuplicate = "merge-train-duplicate" + +// mergeTrainDuplicateCandidate is one open PR declaring `Fixes #N`, as the +// one-PR-per-issue rule reads it (hivecommons/hive#15395). +type mergeTrainDuplicateCandidate struct { + Number int + CreatedAt time.Time + // Green: the head is not blocked (red required check, terminal hold, + // permanent refusal) and GitHub does not report it conflicting. + Green bool +} + +// mergeTrainDuplicateHold is the losing side of a pair: Number is held as +// a duplicate of Winner for Issue. +type mergeTrainDuplicateHold struct { + Number, Winner, Issue int +} + +// mergeTrainDuplicateWinner picks the PR the queue keeps for one issue: +// a green head beats a red one; among equals the earlier-opened PR wins +// (lower number on a tie). So two green PRs keep the earlier; an earlier +// red one yields to a later green one. +func mergeTrainDuplicateWinner(cands []mergeTrainDuplicateCandidate) int { + if len(cands) == 0 { + return 0 + } + best := cands[0] + for _, c := range cands[1:] { + if mergeTrainDuplicateBetter(c, best) { + best = c + } + } + return best.Number +} + +func mergeTrainDuplicateBetter(a, b mergeTrainDuplicateCandidate) bool { + if a.Green != b.Green { + return a.Green + } + if !a.CreatedAt.Equal(b.CreatedAt) && !a.CreatedAt.IsZero() && !b.CreatedAt.IsZero() { + return a.CreatedAt.Before(b.CreatedAt) + } + return a.Number < b.Number +} + +// mergeTrainDuplicateHolds groups the candidates by the issue each one +// closes and holds every PR but the winner per issue. A PR closing several +// issues is held on the first issue it loses. Holds come back sorted by +// PR number so the plan is stable across passes. +func mergeTrainDuplicateHolds(byIssue map[int][]mergeTrainDuplicateCandidate) []mergeTrainDuplicateHold { + held := map[int]mergeTrainDuplicateHold{} + issues := make([]int, 0, len(byIssue)) + for issue := range byIssue { + issues = append(issues, issue) + } + sort.Ints(issues) + for _, issue := range issues { + cands := byIssue[issue] + if len(cands) < 2 { + continue + } + winner := mergeTrainDuplicateWinner(cands) + for _, c := range cands { + if c.Number == winner { + continue + } + if _, done := held[c.Number]; !done { + held[c.Number] = mergeTrainDuplicateHold{Number: c.Number, Winner: winner, Issue: issue} + } + } + } + out := make([]mergeTrainDuplicateHold, 0, len(held)) + for _, h := range held { + out = append(out, h) + } + sort.Slice(out, func(i, j int) bool { return out[i].Number < out[j].Number }) + return out +} + +// mergeTrainClosedIssues reads the same-repo issues a PR's title and body +// declare closed (Fixes / Closes / Resolves #N). +func mergeTrainClosedIssues(pr *gh.PullRequest, displayRepo string) []int { + var out []int + for _, ref := range hgithub.ParseClaimedIssues(pr.GetTitle()+"\n"+pr.GetBody(), displayRepo) { + if ref.Repo == "" || ref.Repo == displayRepo { + out = append(out, ref.Issue) + } + } + return out +} + +// mergeTrainDuplicateReason is the stage reason the sweep records on a held +// duplicate: merge-train:duplicate-of#<winner>:<issue>/0:0/0: +func mergeTrainDuplicateReason(h mergeTrainDuplicateHold) string { + return prstage.MergeTrain{State: prstage.MergeTrainStateDuplicateOf, Front: h.Winner, Position: h.Issue}.Reason() +} + +// planDuplicateHolds runs the one-PR-per-issue rule over the lane's +// candidates before the trains are grouped: the held PRs leave the +// candidate list and the plan remembers who each one yields to. +func (c *Engine) planDuplicateHolds(plan *mergeTrainPlan, owner, repo, displayRepo string, prs []*gh.PullRequest, prefilter func(*gh.PullRequest) string) []*gh.PullRequest { + byIssue := map[int][]mergeTrainDuplicateCandidate{} + for _, pr := range prs { + if pr == nil || pr.GetNumber() <= 0 || !mergeTrainCandidate(prefilter(pr)) { + continue + } + issues := mergeTrainClosedIssues(pr, displayRepo) + if len(issues) == 0 { + continue + } + cand := mergeTrainDuplicateCandidate{ + Number: pr.GetNumber(), + CreatedAt: pr.GetCreatedAt().Time, + Green: !mergeTrainDirty(pr.GetMergeableState()) && !c.mergeTrainHeadBlocked(owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()), + } + for _, issue := range issues { + byIssue[issue] = append(byIssue[issue], cand) + } + } + holds := mergeTrainDuplicateHolds(byIssue) + if len(holds) == 0 { + return prs + } + plan.duplicates = map[int]mergeTrainDuplicateHold{} + for _, h := range holds { + plan.duplicates[h.Number] = h + c.info("merge train: held as duplicate", "repo", displayRepo, "pr", h.Number, "winner", h.Winner, "issue", h.Issue, "lane", plan.lane) + } + kept := make([]*gh.PullRequest, 0, len(prs)) + for _, pr := range prs { + if _, held := plan.duplicates[pr.GetNumber()]; !held { + kept = append(kept, pr) + } + } + return kept +} + +// duplicateOf reports the PR number this one yields to, 0 when it is not +// held as a duplicate this pass. +func (p *mergeTrainPlan) duplicateOf(number int) (mergeTrainDuplicateHold, bool) { + if p == nil || p.duplicates == nil { + return mergeTrainDuplicateHold{}, false + } + h, ok := p.duplicates[number] + return h, ok +} + +// mergeTrainDuplicateSkip records the hold on a duplicate and returns the +// stage reason. +func (c *Engine) mergeTrainDuplicateSkip(plan *mergeTrainPlan, number int) string { + h, _ := plan.duplicateOf(number) + reason := mergeTrainDuplicateReason(h) + c.debug("automerge skip", "repo", plan.displayRepo, "pr", number, "reason", reason, "lane", plan.lane, "winner", strconv.Itoa(h.Winner)) + return reason +} diff --git a/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go b/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go new file mode 100644 index 0000000000..ac42dffe58 --- /dev/null +++ b/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go @@ -0,0 +1,149 @@ +package automerge + +import ( + "context" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + + "github.com/hivecommons/hive/pkg/stageengine/prstage" +) + +// One PR per issue in the merge queue (hivecommons/hive#15395): green +// beats red, earlier beats later, the later-opened duplicate is held. +func TestMergeTrainDuplicateWinner(t *testing.T) { + t0 := time.Date(2026, 10, 29, 10, 0, 0, 0, time.UTC) + cand := func(n int, min int, green bool) mergeTrainDuplicateCandidate { + return mergeTrainDuplicateCandidate{Number: n, CreatedAt: t0.Add(time.Duration(min) * time.Minute), Green: green} + } + for _, tc := range []struct { + name string + cands []mergeTrainDuplicateCandidate + want int + }{ + {"both green: the earlier wins", []mergeTrainDuplicateCandidate{cand(15402, 0, true), cand(15407, 30, true)}, 15402}, + {"both green, listed later first: still the earlier", []mergeTrainDuplicateCandidate{cand(15407, 30, true), cand(15402, 0, true)}, 15402}, + {"earlier red, later green: the later wins", []mergeTrainDuplicateCandidate{cand(15402, 0, false), cand(15407, 30, true)}, 15407}, + {"earlier green, later red: the earlier wins", []mergeTrainDuplicateCandidate{cand(15402, 0, true), cand(15407, 30, false)}, 15402}, + {"both red: the earlier wins", []mergeTrainDuplicateCandidate{cand(15402, 0, false), cand(15407, 30, false)}, 15402}, + {"same instant: the lower number", []mergeTrainDuplicateCandidate{cand(15407, 0, true), cand(15402, 0, true)}, 15402}, + {"three: the earliest green", []mergeTrainDuplicateCandidate{cand(15402, 0, false), cand(15407, 30, true), cand(15410, 60, true)}, 15407}, + {"one candidate", []mergeTrainDuplicateCandidate{cand(15402, 0, false)}, 15402}, + {"none", nil, 0}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := mergeTrainDuplicateWinner(tc.cands); got != tc.want { + t.Fatalf("winner = #%d, want #%d", got, tc.want) + } + }) + } +} + +func TestMergeTrainDuplicateHolds(t *testing.T) { + t0 := time.Date(2026, 10, 29, 10, 0, 0, 0, time.UTC) + byIssue := map[int][]mergeTrainDuplicateCandidate{ + 15396: {{Number: 15402, CreatedAt: t0, Green: true}, {Number: 15407, CreatedAt: t0.Add(time.Hour), Green: true}}, + 15300: {{Number: 15320, CreatedAt: t0, Green: false}, {Number: 15330, CreatedAt: t0.Add(time.Hour), Green: true}}, + 15999: {{Number: 15350, CreatedAt: t0, Green: true}}, + } + got := mergeTrainDuplicateHolds(byIssue) + want := []mergeTrainDuplicateHold{{Number: 15320, Winner: 15330, Issue: 15300}, {Number: 15407, Winner: 15402, Issue: 15396}} + if len(got) != len(want) { + t.Fatalf("holds = %+v, want %+v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("hold[%d] = %+v, want %+v", i, got[i], want[i]) + } + } + reason := mergeTrainDuplicateReason(want[1]) + if reason != "merge-train:duplicate-of#15402:15396/0:0/0:" { + t.Fatalf("reason = %q", reason) + } + m, ok := prstage.ParseMergeTrain(reason) + if !ok || m.State != prstage.MergeTrainStateDuplicateOf || m.Front != 15402 || m.Position != 15396 { + t.Fatalf("parse = %+v ok=%v", m, ok) + } + if s := m.Sentence(); s != "duplicate of #15402 for issue #15396; the merge queue carries one PR per issue — a person or the delegate picks one" { + t.Fatalf("sentence = %q", s) + } +} + +// planDuplicateHolds removes the held PR from the candidates the trains are +// grouped from and remembers who it yields to; a PR with no closing +// keyword, or the only PR for its issue, is untouched. +func TestPlanDuplicateHoldsFiltersCandidates(t *testing.T) { + c := &Engine{} + t0 := time.Date(2026, 10, 29, 10, 0, 0, 0, time.UTC) + pr := func(n int, body string, at time.Time, state string) *gh.PullRequest { + return &gh.PullRequest{Number: gh.Int(n), Title: gh.String("t"), Body: gh.String(body), CreatedAt: &gh.Timestamp{Time: at}, MergeableState: gh.String(state), Head: &gh.PullRequestBranch{SHA: gh.String("sha")}} + } + prs := []*gh.PullRequest{ + pr(15402, "Fixes #15396", t0, "dirty"), + pr(15407, "Fixes #15396", t0.Add(time.Hour), "clean"), + pr(15410, "Refs #15396", t0, "clean"), + pr(15411, "Fixes #15397", t0, "clean"), + } + plan := &mergeTrainPlan{lane: "self", displayRepo: "o/r"} + kept := c.planDuplicateHolds(plan, "o", "r", "o/r", prs, func(*gh.PullRequest) string { return "" }) + if len(kept) != 3 || kept[0].GetNumber() != 15407 { + t.Fatalf("kept = %v", kept) + } + h, held := plan.duplicateOf(15402) + if !held || h.Winner != 15407 || h.Issue != 15396 { + t.Fatalf("hold = %+v held=%v", h, held) + } + if _, held := plan.duplicateOf(15407); held { + t.Fatal("the winner is not held") + } + if _, held := plan.duplicateOf(15410); held { + t.Fatal("a Refs line is not a claim") + } + // A prefilter refusal keeps the PR out of the rule entirely. + plan2 := &mergeTrainPlan{} + kept = c.planDuplicateHolds(plan2, "o", "r", "o/r", prs, func(p *gh.PullRequest) string { + if p.GetNumber() == 15407 { + return "label:hold" + } + return "" + }) + if len(kept) != 4 || plan2.duplicates != nil { + t.Fatalf("kept = %d dup=%v", len(kept), plan2.duplicates) + } +} + +// Through the planner: the held duplicate leaves the train (the train is +// grouped from the survivors), the plan answers duplicateOf for it, and +// the skip reason parses back to the duplicate-of state. +func TestPlanMergeTrainsHoldsSameIssueDuplicate(t *testing.T) { + t0 := time.Date(2026, 10, 29, 9, 0, 0, 0, time.UTC) + api := &mergeTrainAPI{ + files: map[int][]string{15402: {"tracker.go"}, 15407: {"tracker.go"}, 15410: {"tracker.go"}}, + state: map[int]string{15402: "clean", 15407: "clean", 15410: "clean"}, + } + srv := newMergeTrainServer(t, api) + defer srv.Close() + c, _ := newMergeTrainEngine(t, srv.URL, DefaultMergeTrainPolicy, nil) + withBody := func(pr *gh.PullRequest, body string) *gh.PullRequest { pr.Body = gh.String(body); return pr } + prs := []*gh.PullRequest{ + withBody(mtPR(15402, t0, ""), "Fixes #15396"), + withBody(mtPR(15407, t0.Add(time.Hour), ""), "Fixes #15396"), + withBody(mtPR(15410, t0.Add(2*time.Hour), ""), "Fixes #15399"), + } + plan := c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, func(*gh.PullRequest) string { return "" }) + h, held := plan.duplicateOf(15407) + if !held || h.Winner != 15402 || h.Issue != 15396 { + t.Fatalf("hold = %+v held=%v", h, held) + } + if plan.isSibling(15407) || plan.isFront(15407) { + t.Fatal("a held duplicate rides no train") + } + if !plan.isFront(15402) || !plan.isSibling(15410) { + t.Fatalf("train = %+v", plan.groups) + } + mt, ok := prstage.ParseMergeTrain(c.mergeTrainDuplicateSkip(plan, 15407)) + if !ok || mt.State != prstage.MergeTrainStateDuplicateOf || mt.Front != 15402 || mt.Position != 15396 { + t.Fatalf("skip reason parsed %+v ok=%v", mt, ok) + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index f415c08f8c..28ba969fa8 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -1090,6 +1090,13 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge stage := func(reason string, merged bool, err error) { c.recordStage(stageLaneSelf, repo, owner, repoName, pr, reason, merged, err) } + if _, held := train.duplicateOf(number); held { + result.Skipped++ + repoSkipped++ + repoSkipReasons[autoMergeReasonMergeTrainDuplicate]++ + stage(c.mergeTrainDuplicateSkip(train, number), false, nil) + continue + } if train.isSibling(number) { result.Skipped++ repoSkipped++ @@ -1319,6 +1326,11 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg stage := func(reason string, merged bool, err error) { c.recordStage(stageLaneTrusted, displayRepo, owner, repoName, pr, reason, merged, err) } + if _, held := train.duplicateOf(number); held { + result.Skipped++ + stage(c.mergeTrainDuplicateSkip(train, number), false, nil) + continue + } if train.isSibling(number) { result.Skipped++ stage(c.mergeTrainSkip(train, number), false, nil) @@ -1535,7 +1547,7 @@ func (c *Engine) StartSelfAuthoredAutoMergeSweep(ctx context.Context, maxMerges defer close(done) t := time.NewTicker(interval) defer t.Stop() - c.sweepClock.armFloor(c.now().Add(interval)) + c.sweepClock.armFloorEvery(c.now(), interval) // cautionTick is non-zero while the ticker is running at the caution // floor instead of the adaptive interval. var cautionTick time.Duration @@ -1558,7 +1570,7 @@ func (c *Engine) StartSelfAuthoredAutoMergeSweep(ctx context.Context, maxMerges if cautionTick != 0 { tick = cautionTick } - c.sweepClock.armFloor(c.now().Add(tick)) + c.sweepClock.armFloorEvery(c.now(), tick) if gate.skip { continue } @@ -1568,7 +1580,7 @@ func (c *Engine) StartSelfAuthoredAutoMergeSweep(ctx context.Context, maxMerges } if next, changed := nextSelfAuthoredSweepInterval(repos, interval, result); changed { t.Reset(next) - c.sweepClock.armFloor(c.now().Add(next)) + c.sweepClock.armFloorEvery(c.now(), next) c.info("self-authored automerge sweep interval adjusted", "previous_interval", interval, "next_interval", next, diff --git a/src/pkg/github/automerge/sweep_clock.go b/src/pkg/github/automerge/sweep_clock.go index 81e34b356c..7d47320a5a 100644 --- a/src/pkg/github/automerge/sweep_clock.go +++ b/src/pkg/github/automerge/sweep_clock.go @@ -19,6 +19,10 @@ type SweepState struct { // NextFloorAt is when the next full pass is due; zero before the loop // has started. NextFloorAt time.Time + // FloorEvery is the cadence the floor pass was last armed with — the + // dashboard's unit for "one sweep" when it estimates a merge-train + // member's turn (hivecommons/hive#15395). Zero before the loop starts. + FloorEvery time.Duration // Targets names the PRs the running pass evaluates; nil means every PR. Targets SweepTargets } @@ -69,6 +73,17 @@ func (c *SweepClock) armFloor(next time.Time) { c.mu.Unlock() } +// armFloorEvery arms the next floor pass at now+every and records the +// cadence for the dashboard's estimates. +func (c *SweepClock) armFloorEvery(now time.Time, every time.Duration) { + if c == nil { + return + } + c.mu.Lock() + c.st.NextFloorAt, c.st.FloorEvery = now.Add(every), every + c.mu.Unlock() +} + // Snapshot returns a copy of the current state. func (c *SweepClock) Snapshot() SweepState { if c == nil { diff --git a/src/pkg/github/automerge/sweep_clock_test.go b/src/pkg/github/automerge/sweep_clock_test.go index 58532bf0bc..d32ecbfa3e 100644 --- a/src/pkg/github/automerge/sweep_clock_test.go +++ b/src/pkg/github/automerge/sweep_clock_test.go @@ -23,6 +23,10 @@ func TestSweepClock(t *testing.T) { if st := c.Snapshot(); st.Running || st.OnPR("acme/console", 1) || !st.NextFloorAt.Equal(t0.Add(5*time.Minute)) { t.Fatalf("armed, idle: %+v", st) } + c.armFloorEvery(t0, 10*time.Second) + if st := c.Snapshot(); !st.NextFloorAt.Equal(t0.Add(10*time.Second)) || st.FloorEvery != 10*time.Second { + t.Fatalf("armed with cadence: %+v", st) + } targets := SweepTargets{"acme/console": {15042: true}} c.begin("event", targets, t0) diff --git a/src/pkg/stageengine/prstage/merge_train.go b/src/pkg/stageengine/prstage/merge_train.go index 8fe6372b52..07580b0436 100644 --- a/src/pkg/stageengine/prstage/merge_train.go +++ b/src/pkg/stageengine/prstage/merge_train.go @@ -33,8 +33,14 @@ const ( MergeTrainStateRebaseExhausted = "rebase-exhausted" // MergeTrainStateFront is never written by the sweep: the dashboard // synthesises it on the PR the siblings name as their front. - MergeTrainStateFront = "front" - MergeTrainReasonMaxFiles = 4 + MergeTrainStateFront = "front" + // MergeTrainStateDuplicateOf: the PR declares the same `Fixes #N` as + // another open PR the train admitted; it is held out of the train + // behind that PR (Front) until a person or the delegate picks one + // (hivecommons/hive#15395). Position / Size carry the issue number + // as Position and 0 as Size. + MergeTrainStateDuplicateOf = "duplicate-of" + MergeTrainReasonMaxFiles = 4 ) // MergeTrain is the decoded form of a merge-train reason. @@ -182,6 +188,12 @@ func (m MergeTrain) Sentence() string { return "front of the merge train, still conflicting after " + strconv.Itoa(m.MaxAttempts) + " rebase attempts; a person decides (" + slot + files + ")" case MergeTrainStateFront: return "front of the merge train (" + slot + files + ")" + case MergeTrainStateDuplicateOf: + issue := "" + if m.Position > 0 { + issue = " for issue #" + strconv.Itoa(m.Position) + } + return "duplicate of #" + strconv.Itoa(m.Front) + issue + "; the merge queue carries one PR per issue — a person or the delegate picks one" } return "behind #" + strconv.Itoa(m.Front) + " in merge train (" + slot + files + ")" }