diff --git a/changelog.d/fixed-pr-models-attributed-history.md b/changelog.d/fixed-pr-models-attributed-history.md new file mode 100644 index 0000000000..4242828e5a --- /dev/null +++ b/changelog.d/fixed-pr-models-attributed-history.md @@ -0,0 +1 @@ +- The Governor's **PRs by model** card and Operations' **Most effective models** table now count every attributed PR in their `30d` and `all` windows, not only those the closed-PR scan touched in the last 14 days. That scan stops at PRs updated in the last `HIVE_ATTRIBUTED_CLOSED_PR_LOOKBACK` (default 14 days) and kept its results only in memory, so a PR closed and left alone for longer silently dropped out and a restart lost them all. Models lost different shares that way (on one hive 55% of one model's PRs since late October and 90% of another's), so the ranking compared unequal samples. The hive now keeps a history of attributed PRs in `/data/attributed-pr-history.json` (parsed attribution, outcome and rework fields only, never PR bodies) that survives restarts, and a one-time backfill fills it back 30 days (`HIVE_ATTRIBUTED_PR_BACKFILL_LOOKBACK`). The backfill spends at most two list pages and five rework measurements per enumeration pass, only while the GitHub API budget is normal, resumes where it stopped after a restart and makes no calls once done; the per-pass closed-PR scan stays bounded as before. The `coverage` block reports where the history starts (new `closed_prs_since`) and marks `closed_prs_partial` only when a window starts before it. diff --git a/src/cmd/hive/github_client_config.go b/src/cmd/hive/github_client_config.go index f33aeb8d4e..e74a981da4 100644 --- a/src/cmd/hive/github_client_config.go +++ b/src/cmd/hive/github_client_config.go @@ -122,6 +122,10 @@ func (b *boot) applyGitHubClientConfigHooks(client *github.Client) { // Evaluated heads persist so a restart or rebuild neither re-diffs every // open PR nor blanks the Matrix feed until the next sweep (#16276). client.SetSentinelHeadsPath(sentinelHeadsStatePath) + // Attributed PRs persist past the bounded closed-PR scan and a restart, + // so the PRs-by-model 30d/All windows rest on the same sample for every + // model; the store also carries the one-time backfill's cursor. + client.SetAttributedPRHistoryPath(github.AttributedPRHistoryPath) client.SetAPIBudgetThresholds(b.cfg.GitHub.APIReserve, b.cfg.GitHub.APICritical) // The conditional-request cache is process-wide, not per client; size it // from github.etag_cache_max_mb / _max_entries so a hive that evicts diff --git a/src/docs/admin-mcp.md b/src/docs/admin-mcp.md index cca0392c7a..f2004fe8b6 100644 --- a/src/docs/admin-mcp.md +++ b/src/docs/admin-mcp.md @@ -488,8 +488,9 @@ capped at 256 KiB. Other notes: - `effective_models_read` also returns `coverage`. It says how far back the - closed-PR scan reaches and whether PR or run history is partial for the - window, so an `all` answer is not mistaken for lifetime totals. + hive's stored attributed PR history is complete (`closed_prs_since`) and + whether PR or run history is partial for the window, so an `all` answer + is not mistaken for lifetime totals. - None of them carries cost: `spend_read` has spend by model. **Meta tools** diff --git a/src/docs/api-reference/agents-and-controls.md b/src/docs/api-reference/agents-and-controls.md index 379e775c78..cb308dd16a 100644 --- a/src/docs/api-reference/agents-and-controls.md +++ b/src/docs/api-reference/agents-and-controls.md @@ -22,7 +22,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `POST` | `/api/unpin/{agent}/{dimension}` | Owner only | Unpin | `pkg/dashboard/api.go:149` | | `POST` | `/api/restart/{agent}` | Owner only | Restart | `pkg/dashboard/api.go:153` | | `GET` | `/api/model-advisor` | Dashboard auth/session | Model Advisor | `pkg/dashboard/api.go:171` | -| `GET` | `/api/governor/pr-models` | Dashboard auth/session | Agent-authored PR distribution by normalized attribution model/backend for `window=7d`, `30d`, or `all`, including per-model rework stats, Wilson 95% `*_ci` intervals on the rates (omitted when the denominator is 0), a `coverage` block (closed-PR lookback and run-log partiality, as on `/api/contribute/effective-models`), `earliest_pr_created_at`, the top 10 most-reworked PRs, and per-bucket `hive_cost` (`usd`, `launches`, `prs_with_cost`, `usd_per_merged_pr`: the hive's estimated review/fix spend on that model's PRs from the cached `/api/launch-cost` snapshot; `usd` is `null`, not `0`, when nothing was attributed; omitted while the snapshot is not ready) with a top-level `hive_cost_coverage` (`ready`, `window_hours`, `unattributed_usd`, `backend_unsupported_usd`, `disclaimer`). Optional `model=` (matched after `modelfamily.Canonical`, so raw spellings such as `anthropic/claude-opus-5-5` work) adds `prs[]`: every PR in the window in that model's bucket, with the `most_reworked` fields plus `outcome` (`merged` / `closed_unmerged` / `open`), `agent`, `created_at`, `merged_at` and `first_pass`, sorted by review rounds + fix attempts then newest, capped at 200 with `prs_truncated: true` past that. An unknown model returns `prs: []`; without `model` the response is unchanged | `pkg/dashboard/api.go:172` | +| `GET` | `/api/governor/pr-models` | Dashboard auth/session | Agent-authored PR distribution by normalized attribution model/backend for `window=7d`, `30d`, or `all`, including per-model rework stats, Wilson 95% `*_ci` intervals on the rates (omitted when the denominator is 0), a `coverage` block (how far back the stored attributed PR history is complete, `closed_prs_since`, and closed-PR and run-log partiality, as on `/api/contribute/effective-models`), `earliest_pr_created_at`, the top 10 most-reworked PRs, and per-bucket `hive_cost` (`usd`, `launches`, `prs_with_cost`, `usd_per_merged_pr`: the hive's estimated review/fix spend on that model's PRs from the cached `/api/launch-cost` snapshot; `usd` is `null`, not `0`, when nothing was attributed; omitted while the snapshot is not ready) with a top-level `hive_cost_coverage` (`ready`, `window_hours`, `unattributed_usd`, `backend_unsupported_usd`, `disclaimer`). Optional `model=` (matched after `modelfamily.Canonical`, so raw spellings such as `anthropic/claude-opus-5-5` work) adds `prs[]`: every PR in the window in that model's bucket, with the `most_reworked` fields plus `outcome` (`merged` / `closed_unmerged` / `open`), `agent`, `created_at`, `merged_at` and `first_pass`, sorted by review rounds + fix attempts then newest, capped at 200 with `prs_truncated: true` past that. An unknown model returns `prs: []`; without `model` the response is unchanged | `pkg/dashboard/api.go:172` | | `GET` | `/api/reviewer/accuracy` | Dashboard auth/session | Reviewer verdict calibration for the configured window: false-approve/false-block rates and confidence buckets per perspective and reviewer model | `pkg/dashboard/api.go:239` | | `GET` | `/api/review/queue` | Dashboard auth/session | PR review queue: every open PR (agent and contributor, actionable and held) ranked by triage class, confidence band, CI state and age, with per-PR `reasons`; unreviewed items carry `pending_since` and `redispatch_count` from the review dispatch state; each entry carries `stage` `{stage, entered_at, last_activity_at, quiet_for, stalled, waiting_on_hive, past_dwell, history[]}` from the stage-label store (`stalled`/`past_dwell` follow `governor.labels.stage.dwell` and `stalled.require_quiet`; `history[]` is `{from, to, at, reason, regressed}`, newest last, capped at 8) and the top-level `hive_held` counts PRs past their stage dwell while waiting on the hive (`waiting_on_hive` and `past_dwell` both true); paged with `limit` (1-200, default 50) and `offset` | `pkg/dashboard/api.go:533` | | `POST` | `/api/review/queue/{owner}/{repo}/{number}/superseded/close` | Owner only | Close a superseded PR now (grace window or not): posts the close comment, adds `hive/superseded`, closes it | `pkg/dashboard/api.go:536` | diff --git a/src/docs/contributor-relay.md b/src/docs/contributor-relay.md index e89976bf29..28fbb76184 100644 --- a/src/docs/contributor-relay.md +++ b/src/docs/contributor-relay.md @@ -1233,7 +1233,7 @@ After a contributor completes an issue, the hub keeps that issue out of the queu ### Queue hold and priority -The **Operations** tab also includes a public-safe, collapsible **Most effective models** panel, collapsed by default so the work, runs, decisions and queue cards sit near the top; its header still shows the ranked-count summary once the panel has been opened. It reads the same aggregate PR rework data as the Governor PRs-by-model view plus the contributor task-run log, then ranks model + CLI pairs for `7d`, `30d`, or `all` by visible measures: merged PRs, first-pass merge rate, average review rounds, average fix attempts, verified-PR run rate, failure rate, and completed-without-PR (“nothing to ship”) rate. Rows below `HIVE_CONTRIBUTE_EFFECTIVE_MODELS_MIN_PRS` merged PRs (default `5`) are shown under “Not enough data yet” instead of being ranked, and the response is aggregate-only: no contributor usernames, pane output, tokens, or per-contributor breakdowns. The panel only requests `/api/contribute/effective-models` once it is expanded, and the choice is remembered in browser localStorage in both directions (`1` collapsed, `0` expanded; no stored value means the collapsed default). Run-derived rates show `—` for rows with no recorded runs (they come from the contributor task-run log only), and the runtime label reflects where inference runs: a Pi row is `local` only for a local provider such as `lemonade/` or `ollama`, `hosted` for a hosted provider prefix such as `openai-codex/`, and `unknown` otherwise. The response's `coverage` object states the closed-PR scan lookback (`HIVE_ATTRIBUTED_CLOSED_PR_LOOKBACK`, default 14 days) and the oldest retained run (live log plus its rotated `.1` file), and flags `30d`/`all` windows that reach past retained history as partial; the panel shows that coverage under the table. +The **Operations** tab also includes a public-safe, collapsible **Most effective models** panel, collapsed by default so the work, runs, decisions and queue cards sit near the top; its header still shows the ranked-count summary once the panel has been opened. It reads the same aggregate PR rework data as the Governor PRs-by-model view plus the contributor task-run log, then ranks model + CLI pairs for `7d`, `30d`, or `all` by visible measures: merged PRs, first-pass merge rate, average review rounds, average fix attempts, verified-PR run rate, failure rate, and completed-without-PR (“nothing to ship”) rate. Rows below `HIVE_CONTRIBUTE_EFFECTIVE_MODELS_MIN_PRS` merged PRs (default `5`) are shown under “Not enough data yet” instead of being ranked, and the response is aggregate-only: no contributor usernames, pane output, tokens, or per-contributor breakdowns. The panel only requests `/api/contribute/effective-models` once it is expanded, and the choice is remembered in browser localStorage in both directions (`1` collapsed, `0` expanded; no stored value means the collapsed default). Run-derived rates show `—` for rows with no recorded runs (they come from the contributor task-run log only), and the runtime label reflects where inference runs: a Pi row is `local` only for a local provider such as `lemonade/` or `ollama`, `hosted` for a hosted provider prefix such as `openai-codex/`, and `unknown` otherwise. Closed PRs come from the hive's attributed PR history (`/data/attributed-pr-history.json`), which keeps every attributed PR the enumeration has seen after the bounded live scan (`HIVE_ATTRIBUTED_CLOSED_PR_LOOKBACK`, default 14 days) stops listing it, and which a one-time backfill fills back to `HIVE_ATTRIBUTED_PR_BACKFILL_LOOKBACK` (default 30 days). The response's `coverage` object states how far back that history is complete (`closed_prs_since` and `closed_pr_lookback_days`) and the oldest retained run (live log plus its rotated `.1` file), and flags windows that start before retained history as partial: `30d` until the backfill finishes, `all` unless the history reaches back to a repo's first PR. The panel shows that coverage under the table. The dashboard **Governor** card's **PRs by model** section uses that same model-effectiveness aggregation for the same `7d`, `30d`, and `all` windows. It keeps the merged/open/closed bar for PR volume, adds compact columns for merged count, first-pass merge rate, verified-PR run rate, failure rate, and “nothing to ship” rate, and badges ranked models that clear the merged-PR threshold. Operators can toggle the row order between effectiveness rank (default) and raw PR count. diff --git a/src/docs/env-vars.md b/src/docs/env-vars.md index 66694bbc12..2cb47e5443 100644 --- a/src/docs/env-vars.md +++ b/src/docs/env-vars.md @@ -69,7 +69,8 @@ behavior. [Maintenance limitations](#keeping-this-reference-current) still apply | `HIVE_SCANNER_BUSY_BRANCH_MINUTES` | No | `60` | Busy-branch window for the repair queues ([#18488](https://github.com/hivecommons/hive/issues/18488)). A red or conflicted PR the hive App did not open is listed as "suggest, do not push" instead of as a push target while its head is younger than this many minutes or a claim by another worker covers the PR or a linked issue; the agent posts the fix as one suggestion comment for the worker to apply as its own signed commit. Whole minutes; unset, unparsable or non-positive values keep `60`. | | `HIVE_RELEASE_FEED_INTERVAL` | No | `governor.release_feeds.interval_s`, else `15m` | Poll interval of the release-feed watcher ([#18494](https://github.com/hivecommons/hive/issues/18494)) as a Go duration. An explicit `interval_s` wins over it; never below `1m`. Invalid or non-positive values fall back to `15m`. See [release-feeds.md](release-feeds.md). | | `HIVE_RELEASE_FEED_COOLDOWN` | No | `governor.release_feeds.cooldown_s` / per-agent `cooldown_s`, else `1h` | Default minimum gap between two release-feed kicks of one agent, as a Go duration. The YAML keys win over it. See [release-feeds.md](release-feeds.md#cooldown). | -| `HIVE_ATTRIBUTED_CLOSED_PR_LOOKBACK` | No | `14d` | Bounds the per-repo closed-PR attribution scan that feeds dashboard outcome/rework metrics during PR enumeration. The scan lists closed PRs sorted by `updated_at` descending, stops when results are older than this window, and also stops after five pages so large repositories are never walked from history on every governor tick. Values accept Go durations such as `336h`, or day counts such as `14`/`14d`; invalid or non-positive values fall back to `14d`. | +| `HIVE_ATTRIBUTED_CLOSED_PR_LOOKBACK` | No | `14d` | Bounds the per-repo closed-PR attribution scan that feeds dashboard outcome/rework metrics during PR enumeration. The scan lists closed PRs sorted by `updated_at` descending, stops when results are older than this window, and also stops after five pages so large repositories are never walked from history on every governor tick. Values accept Go durations such as `336h`, or day counts such as `14`/`14d`; invalid or non-positive values fall back to `14d`. Each scan's attributed PRs are also written to the attributed PR history (`/data/attributed-pr-history.json`), so a PR stays in the PRs-by-model and Most effective models windows after this scan stops listing it, and across restarts. | +| `HIVE_ATTRIBUTED_PR_BACKFILL_LOOKBACK` | No | `30d` | How far back, by PR creation, the one-time backfill of the attributed PR history walks each repo's closed PRs (newest first). It runs once per repo when the history is new, spends at most two list pages and five rework measurements per enumeration pass across all repos and only while the GitHub API budget is normal, resumes from its saved cursor after a restart, and makes no calls once complete. Until it finishes, windows that reach past the live scan's lookback report `closed_prs_partial`. Same value syntax as `HIVE_ATTRIBUTED_CLOSED_PR_LOOKBACK`. | | `HIVE_LEVEL` | No | config/pack value | ACMM level bootstrap/override used by hosted flows and the entrypoint pack selection. | | `HIVE_ID` | No | config or generated id | Stable hive/spoke identifier override; passed through to launched agents. | | `HIVE_CLUSTER_ID` | No | config or hub-provisioned value | Hosted cluster identifier override. | diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index 2a54bdf8d9..8a16197ab8 100644 --- a/src/docs/scale-envelope.md +++ b/src/docs/scale-envelope.md @@ -34,7 +34,7 @@ ones to: 2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.** This holds today and is easy to assume backwards: - Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied - (`RankActionableIssues`, `src/pkg/github/client.go:5652-5662`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:5686-5696`, descending `AgeMinutes` within each tier). - PRs are sorted by **review class then oldest-first within class** (`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`; fixes → refactors/docs → tests). diff --git a/src/pkg/adminmcp/tools_model_effectiveness.go b/src/pkg/adminmcp/tools_model_effectiveness.go index c861a1e597..2aa200820a 100644 --- a/src/pkg/adminmcp/tools_model_effectiveness.go +++ b/src/pkg/adminmcp/tools_model_effectiveness.go @@ -51,7 +51,7 @@ const prModelsReadDescription = "Read Governor → \"PRs by model\" through GET const contributorModelStatsReadDescription = "Read the contributor run stats by model through GET /api/contribute/run-stats/models: per model, runs, verified_pr_share (runs that ended in a verified PR) and failure_rate, over window_days. days is 0-365 (default 7); 0 reads every recorded run. Rows are sorted by model name. limit caps the models rows and offset skips rows; a capped answer says so in _admin_mcp and gives next_offset. Read-only." -const effectiveModelsReadDescription = "Read the Operations page's \"Most effective models\" table through GET /api/contribute/effective-models, the endpoint the panel reads. window is 7d (default), 30d or all. filter is all (default), hive (PRs the hive's own agents opened; these have no contributor runs, so run columns are empty) or contributor (only PRs a verified contributor run produced, with those runs). Returns window, filter, min_merged_prs, ranked (models with at least min_merged_prs merged PRs, best score first, with effectiveness_rank), insufficient (the rest) and coverage (closed_pr_lookback_days, closed_prs_partial, runs_since, runs_partial: how much history the window really covers). Each row has model, backends, sources, runtime, score, prs, merged_prs, first-pass merges and rate, average review rounds and fix attempts, runs, and verified-PR, failed and nothing-to-ship run counts and rates. ranked is returned whole; limit caps the insufficient rows and offset skips rows of it. Read-only." +const effectiveModelsReadDescription = "Read the Operations page's \"Most effective models\" table through GET /api/contribute/effective-models, the endpoint the panel reads. window is 7d (default), 30d or all. filter is all (default), hive (PRs the hive's own agents opened; these have no contributor runs, so run columns are empty) or contributor (only PRs a verified contributor run produced, with those runs). Returns window, filter, min_merged_prs, ranked (models with at least min_merged_prs merged PRs, best score first, with effectiveness_rank), insufficient (the rest) and coverage (closed_pr_lookback_days, closed_prs_partial, closed_prs_since, runs_since, runs_partial: how much history the window really covers; closed_prs_since is when the hive's stored PR history starts). Each row has model, backends, sources, runtime, score, prs, merged_prs, first-pass merges and rate, average review rounds and fix attempts, runs, and verified-PR, failed and nothing-to-ship run counts and rates. ranked is returned whole; limit caps the insufficient rows and offset skips rows of it. Read-only." // ModelEffectivenessTool reports whether tool is one of the model // effectiveness reads. diff --git a/src/pkg/dashboard/api_governor_pr_models.go b/src/pkg/dashboard/api_governor_pr_models.go index 64492bba63..a5e9428b45 100644 --- a/src/pkg/dashboard/api_governor_pr_models.go +++ b/src/pkg/dashboard/api_governor_pr_models.go @@ -264,19 +264,24 @@ func (s *Server) handleGovernorPRModels(w http.ResponseWriter, r *http.Request) window := normalizeGovernorPRModelsWindow(r.URL.Query().Get("window")) minMergedPRs := effectiveModelsMinMergedPRs() actionable := s.lastActionableForPRModels() - resp := aggregateGovernorPRModels(actionable.PRs.Attributed, window, time.Now()) - applyGovernorPRModelCloseReasons(&resp, actionable.PRs.Attributed, governorPRModelsSupersededKeys(actionable.PRs.Attributed, actionable.PRs.Items), time.Now()) + // Live attributed PRs plus the persisted history's closed ones, so the + // 30d/All windows keep PRs the bounded live scan no longer lists. + attributed, history := s.prModelsAttributedPRs(actionable) + resp := aggregateGovernorPRModels(attributed, window, time.Now()) + applyGovernorPRModelCloseReasons(&resp, attributed, governorPRModelsSupersededKeys(attributed, actionable.PRs.Items), time.Now()) resp.FixerQueue = s.governorFixerQueue() now := time.Now() - runs, rotated, _ := readEffectiveModelRuns(s.effectiveModelTaskRunLogPath(), governorPRModelsWindowDuration(window)) - resp.Coverage = effectiveModelsCoverage(governorPRModelsWindowDuration(window), ghpkg.AttributedClosedPRLookback(), runs, rotated, now) + windowDur := governorPRModelsWindowDuration(window) + runs, rotated, _ := readEffectiveModelRuns(s.effectiveModelTaskRunLogPath(), windowDur) + resp.Coverage = effectiveModelsCoverage(windowDur, ghpkg.AttributedClosedPRLookback(), runs, rotated, now) + applyAttributedPRHistoryCoverage(&resp.Coverage, history, attributed, windowDur, now) effective := aggregateEffectiveModelsFromPRAggregation(resp, aggregateEffectiveRunStats(runs, effectiveModelsFilterAll), effectiveModelsFilterAll, minMergedPRs) applyGovernorPRModelEffectiveness(&resp, effective, minMergedPRs) launchCost, launchCostReady := s.launchCostSnapshotForPRModels() - applyGovernorPRModelHiveCost(&resp, actionable.PRs.Attributed, launchCost, launchCostReady, now) + applyGovernorPRModelHiveCost(&resp, attributed, launchCost, launchCostReady, now) w.Header().Set("Content-Type", "application/json") if model, ok := governorPRModelsQueryModel(r); ok { - _ = json.NewEncoder(w).Encode(withGovernorPRModelPRs(resp, actionable.PRs.Attributed, model, now)) + _ = json.NewEncoder(w).Encode(withGovernorPRModelPRs(resp, attributed, model, now)) return } _ = json.NewEncoder(w).Encode(resp) diff --git a/src/pkg/dashboard/api_governor_pr_models_history.go b/src/pkg/dashboard/api_governor_pr_models_history.go new file mode 100644 index 0000000000..dca9fc236f --- /dev/null +++ b/src/pkg/dashboard/api_governor_pr_models_history.go @@ -0,0 +1,83 @@ +package dashboard + +import ( + "time" + + ghpkg "github.com/hivecommons/hive/pkg/github" +) + +// The PRs-by-model and Most-effective-models cards read the persisted +// attributed PR history (pkg/github/attributed_pr_history.go) under the live +// enumeration. The live records win for any PR both hold: they carry the +// freshest state, and they are the only source for open PRs. The history adds +// the closed PRs the bounded live scan no longer lists, so a 30d window counts +// a PR closed and left alone for longer than the scan's lookback. + +// prModelsAttributedPRs is the attributed-PR sample behind both cards and the +// history's coverage. Coverage.Available is false without a GitHub client or +// with no history store; the cards then rest on the live scan, as before. +func (s *Server) prModelsAttributedPRs(actionable *ghpkg.ActionableResult) ([]ghpkg.PullRequest, ghpkg.AttributedPRCoverage) { + var live []ghpkg.PullRequest + if actionable != nil { + live = actionable.PRs.Attributed + } + if s == nil || s.deps == nil || s.deps.GHClient == nil { + return live, ghpkg.AttributedPRCoverage{} + } + stored, cov := s.deps.GHClient.AttributedPRHistory() + return mergeAttributedPRHistory(live, stored), cov +} + +// mergeAttributedPRHistory appends to live the stored PRs it does not already +// hold, keyed the way the cards key PRs (effectivePRKey of effectivePRRepo). +func mergeAttributedPRHistory(live, stored []ghpkg.PullRequest) []ghpkg.PullRequest { + if len(stored) == 0 { + return live + } + seen := make(map[string]bool, len(live)+len(stored)) + out := make([]ghpkg.PullRequest, 0, len(live)+len(stored)) + for _, pr := range live { + seen[effectivePRKey(effectivePRRepo(pr), pr.Number)] = true + out = append(out, pr) + } + for _, pr := range stored { + key := effectivePRKey(effectivePRRepo(pr), pr.Number) + if seen[key] { + continue + } + seen[key] = true + out = append(out, pr) + } + return out +} + +// applyAttributedPRHistoryCoverage sets the closed-PR half of cov from the +// history's real reach. Windows are by PR creation, and the history holds +// every attributed PR created at or after hist.Since, so a window is partial +// only when it starts before that (All: unless the history is full). The +// lookback days and closed_prs_since report that start; for a full history it +// is the oldest PR held. Without a history cov is left as the live scan's. +func applyAttributedPRHistoryCoverage(cov *contributeEffectiveModelsCoverage, hist ghpkg.AttributedPRCoverage, prs []ghpkg.PullRequest, window time.Duration, now time.Time) { + if cov == nil || !hist.Available { + return + } + since := hist.Since + if hist.Full { + cov.ClosedPRsPartial = false + since = time.Time{} + for _, pr := range prs { + if pr.HiveAttributed && !pr.CreatedAt.IsZero() && (since.IsZero() || pr.CreatedAt.Before(since)) { + since = pr.CreatedAt + } + } + } else { + cov.ClosedPRsPartial = window <= 0 || now.Add(-window).Before(since) + } + if since.IsZero() { + return + } + cov.ClosedPRsSince = since.UTC().Format(time.RFC3339) + if days := int(now.Sub(since) / (24 * time.Hour)); days > cov.ClosedPRLookbackDays { + cov.ClosedPRLookbackDays = days + } +} diff --git a/src/pkg/dashboard/api_governor_pr_models_history_test.go b/src/pkg/dashboard/api_governor_pr_models_history_test.go new file mode 100644 index 0000000000..773cb79987 --- /dev/null +++ b/src/pkg/dashboard/api_governor_pr_models_history_test.go @@ -0,0 +1,196 @@ +package dashboard + +import ( + "encoding/json" + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + "time" + + ghpkg "github.com/hivecommons/hive/pkg/github" +) + +// storedClosedPRs are three merged opus PRs closed 3, 16 and 25 days ago (each +// created a day earlier): all inside a 30d window, the last two past the live +// scan's 14-day lookback. +func storedClosedPRs(now time.Time) []ghpkg.PullRequest { + var out []ghpkg.PullRequest + for i, days := range []int{3, 16, 25} { + closed := now.AddDate(0, 0, -days) + out = append(out, ghpkg.PullRequest{ + Repo: "org/repo", Number: i + 1, URL: fmt.Sprintf("https://github.com/org/repo/pull/%d", i+1), + HiveAttributed: true, HiveModel: "opus", HiveBackend: "copilot", State: "closed", + CreatedAt: closed.AddDate(0, 0, -1), ClosedAt: closed, MergedAt: closed, + Rework: ghpkg.PRReworkStats{FirstPass: true}, + }) + } + return out +} + +func TestPRModels30dCountsStoredClosedPRsPastLiveLookback(t *testing.T) { + now := time.Date(2026, 11, 19, 12, 0, 0, 0, time.UTC) + stored := storedClosedPRs(now) + // The live scan still lists the newest one; the merge must not double it. + live := []ghpkg.PullRequest{stored[0]} + prs := mergeAttributedPRHistory(live, stored) + if len(prs) != 3 { + t.Fatalf("merged PRs = %d, want 3 (live wins, no duplicates)", len(prs)) + } + + got := aggregateGovernorPRModels(prs, governorPRModelsWindow30d, now) + if got.Total != 3 || len(got.Buckets) != 1 || got.Buckets[0].Merged != 3 || got.Buckets[0].Rework.FirstPassMerged != 3 { + t.Fatalf("30d PRs-by-model = %+v, want all three merged PRs counted", got) + } + eff := aggregateContributeEffectiveModels(prs, nil, governorPRModelsWindow30d, effectiveModelsFilterAll, 1, now) + if len(eff.Ranked) != 1 || eff.Ranked[0].MergedPRs != 3 || eff.Ranked[0].FirstPassMerged != 3 { + t.Fatalf("30d effective models = %+v, want one ranked row over three merged PRs", eff) + } +} + +func TestApplyAttributedPRHistoryCoverage(t *testing.T) { + now := time.Date(2026, 11, 19, 12, 0, 0, 0, time.UTC) + day := 24 * time.Hour + month := 30 * day + base := func() contributeEffectiveModelsCoverage { + return effectiveModelsCoverage(month, 14*day, nil, false, now) + } + if cov := base(); !cov.ClosedPRsPartial { + t.Fatalf("live-only 30d coverage = %+v, want partial (control)", cov) + } + + covered := base() + applyAttributedPRHistoryCoverage(&covered, ghpkg.AttributedPRCoverage{Available: true, Since: now.Add(-31 * day)}, nil, month, now) + if covered.ClosedPRsPartial || covered.ClosedPRLookbackDays != 31 || covered.ClosedPRsSince != now.Add(-31*day).Format(time.RFC3339) { + t.Fatalf("coverage with the store back 31 days = %+v, want 30d complete since that date", covered) + } + + short := base() + applyAttributedPRHistoryCoverage(&short, ghpkg.AttributedPRCoverage{Available: true, Since: now.Add(-14 * day)}, nil, month, now) + if !short.ClosedPRsPartial || short.ClosedPRsSince == "" { + t.Fatalf("coverage during the backfill = %+v, want 30d partial", short) + } + + all := effectiveModelsCoverage(0, 14*day, nil, false, now) + applyAttributedPRHistoryCoverage(&all, ghpkg.AttributedPRCoverage{Available: true, Since: now.Add(-31 * day)}, nil, 0, now) + if !all.ClosedPRsPartial { + t.Fatalf("All coverage = %+v, want partial before the backfill horizon", all) + } + + full := effectiveModelsCoverage(0, 14*day, nil, false, now) + applyAttributedPRHistoryCoverage(&full, ghpkg.AttributedPRCoverage{Available: true, Full: true}, storedClosedPRs(now), 0, now) + if full.ClosedPRsPartial || full.ClosedPRsSince != now.AddDate(0, 0, -26).Format(time.RFC3339) { + t.Fatalf("All coverage with full history = %+v, want complete since the oldest PR", full) + } + + none := base() + applyAttributedPRHistoryCoverage(&none, ghpkg.AttributedPRCoverage{}, nil, month, now) + if none != base() { + t.Fatalf("coverage without a store = %+v, want the live scan's unchanged", none) + } +} + +// seedAttributedPRHistory writes a history file (the format +// pkg/github/attributed_pr_history.go reads) holding storedClosedPRs for repo +// "repo", with the one-time backfill done or still running. +func seedAttributedPRHistory(t *testing.T, now time.Time, backfillDone bool) string { + t.Helper() + ts := func(tm time.Time) string { return tm.UTC().Format(time.RFC3339Nano) } + records := map[string]any{} + for _, pr := range storedClosedPRs(now) { + records[fmt.Sprintf("repo#%d", pr.Number)] = map[string]any{ + "repo": "repo", "number": pr.Number, "url": pr.URL, "model": pr.HiveModel, "backend": pr.HiveBackend, + "state": "closed", "created_at": ts(pr.CreatedAt), "closed_at": ts(pr.ClosedAt), "merged_at": ts(pr.MergedAt), + "rework": map[string]any{"first_pass": true}, "rework_known": true, + } + } + scan := now.Add(-time.Hour) + backfill := map[string]any{"started_at": ts(scan), "horizon": ts(scan.Add(-31 * 24 * time.Hour)), "next_page": 4} + if backfillDone { + backfill["listed"], backfill["done"] = true, true + } + file := map[string]any{ + "schema_version": 1, + "generated_at": ts(now), + "repos": map[string]any{"repo": map[string]any{ + "last_scan_at": ts(scan), "run_start": ts(scan), "run_floor": ts(scan.Add(-14 * 24 * time.Hour)), "backfill": backfill, + }}, + "records": records, + } + raw, err := json.Marshal(file) + if err != nil { + t.Fatal(err) + } + path := filepath.Join(t.TempDir(), ghpkg.AttributedPRHistoryFile) + if err := os.WriteFile(path, raw, 0o600); err != nil { + t.Fatal(err) + } + return path +} + +func prModelsHistoryServer(t *testing.T, historyPath string) *Server { + t.Helper() + s := covApiServer(t) + dir := t.TempDir() + s.SetContributorsDir(dir) + if s.contributeHub != nil { + s.contributeHub.taskRunLogFile = filepath.Join(dir, taskRunLogFileName) + } + client := ghpkg.NewClientForTest("http://127.0.0.1:1", "org", []string{"repo"}, slog.New(slog.NewTextHandler(io.Discard, nil))) + client.SetAttributedPRHistoryPath(historyPath) + s.deps.GHClient = client + s.deps.Scheduler = metricsSchedulerStub{actionable: &ghpkg.ActionableResult{}} + return s +} + +func TestPRModelsHandlersReadAttributedPRHistory(t *testing.T) { + now := time.Now().UTC() + for _, tc := range []struct { + name string + done bool + window string + wantPartial bool + }{ + {"backfill done, 30d", true, "30d", false}, + {"backfill running, 30d", false, "30d", true}, + {"backfill done, all", true, "all", true}, + } { + t.Run(tc.name, func(t *testing.T) { + s := prModelsHistoryServer(t, seedAttributedPRHistory(t, now, tc.done)) + + rec := httptest.NewRecorder() + s.handleGovernorPRModels(rec, httptest.NewRequest(http.MethodGet, "/api/governor/pr-models?window="+tc.window, nil)) + var models governorPRModelsResponse + if err := json.Unmarshal(rec.Body.Bytes(), &models); err != nil { + t.Fatalf("decode pr-models: %v body %s", err, rec.Body.String()) + } + if models.Total != 3 || len(models.Buckets) != 1 || models.Buckets[0].Merged != 3 { + t.Fatalf("pr-models = %+v, want the three stored merged PRs", models) + } + if models.Coverage.ClosedPRsPartial != tc.wantPartial || models.Coverage.ClosedPRsSince == "" { + t.Fatalf("pr-models coverage = %+v, want partial=%v with closed_prs_since", models.Coverage, tc.wantPartial) + } + + rec = httptest.NewRecorder() + s.handleContributeEffectiveModels(rec, httptest.NewRequest(http.MethodGet, "/api/contribute/effective-models?window="+tc.window, nil)) + var eff contributeEffectiveModelsResponse + if err := json.Unmarshal(rec.Body.Bytes(), &eff); err != nil { + t.Fatalf("decode effective-models: %v body %s", err, rec.Body.String()) + } + merged := 0 + for _, row := range append(eff.Ranked, eff.Insufficient...) { + merged += row.MergedPRs + } + if merged != 3 { + t.Fatalf("effective-models = %+v, want three merged PRs", eff) + } + if eff.Coverage.ClosedPRsPartial != tc.wantPartial { + t.Fatalf("effective-models coverage = %+v, want partial=%v", eff.Coverage, tc.wantPartial) + } + }) + } +} diff --git a/src/pkg/dashboard/contribute_effective_models.go b/src/pkg/dashboard/contribute_effective_models.go index e2efa574fe..f47dc20381 100644 --- a/src/pkg/dashboard/contribute_effective_models.go +++ b/src/pkg/dashboard/contribute_effective_models.go @@ -38,12 +38,16 @@ type contributeEffectiveModelsResponse struct { } // contributeEffectiveModelsCoverage reports how much history fed the table. -// Closed attributed PRs come from a bounded rolling scan and runs from the -// task-run log plus its single rotated predecessor, so a 30d/All window can -// ask for more than is retained; the *_partial flags say so explicitly. +// Closed attributed PRs come from the persisted attributed PR history (or, +// without one, the bounded rolling scan) and runs from the task-run log plus +// its single rotated predecessor, so a 30d/All window can ask for more than +// is retained; the *_partial flags say so explicitly. ClosedPRsSince is the +// history's coverage start (applyAttributedPRHistoryCoverage), omitted +// without a history. type contributeEffectiveModelsCoverage struct { ClosedPRLookbackDays int `json:"closed_pr_lookback_days"` ClosedPRsPartial bool `json:"closed_prs_partial"` + ClosedPRsSince string `json:"closed_prs_since,omitempty"` RunsSince string `json:"runs_since,omitempty"` RunsPartial bool `json:"runs_partial"` } @@ -97,12 +101,13 @@ func (s *Server) handleContributeEffectiveModels(w http.ResponseWriter, r *http. } } - prs := s.lastActionableForPRModels().PRs.Attributed + prs, history := s.prModelsAttributedPRs(s.lastActionableForPRModels()) windowDur := governorPRModelsWindowDuration(window) runs, rotated, _ := readEffectiveModelRuns(s.effectiveModelTaskRunLogPath(), windowDur) now := time.Now() resp := aggregateContributeEffectiveModels(prs, runs, window, filter, minMergedPRs, now) resp.Coverage = effectiveModelsCoverage(windowDur, ghpkg.AttributedClosedPRLookback(), runs, rotated, now) + applyAttributedPRHistoryCoverage(&resp.Coverage, history, prs, windowDur, now) w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(resp) } diff --git a/src/pkg/github/attributed_pr_history.go b/src/pkg/github/attributed_pr_history.go new file mode 100644 index 0000000000..4a87908329 --- /dev/null +++ b/src/pkg/github/attributed_pr_history.go @@ -0,0 +1,1031 @@ +package github + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "net/http" + "os" + "path/filepath" + "sort" + "strings" + "sync" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// Attributed PR history: the durable record behind the PRs-by-model and +// Most-effective-models cards' 30d and All windows. +// +// The closed attributed-PR scan (fetchAttributedClosedPRs) is bounded to the +// last attributedClosedPRDefaultLookback of updates and five pages per pass +// (#9253/#9255), and its result lived only in memory. A PR closed and left +// alone for longer than the lookback dropped out of every window and a restart +// lost them all, so models lost different shares of their PRs depending on +// when they were used. This store keeps every attributed PR the enumeration +// sees, open or closed, and a one-time backfill walks the closed history back +// to the backfill horizon. +// +// Format: one JSON file (AttributedPRHistoryPath) written atomically +// (tmp + rename), like the PR rework cache beside it. It holds parsed +// attribution and outcome fields only, never a PR body. The live scan stays +// exactly as bounded as before; only the backfill reaches further, and only +// within its own per-pass budget. +const ( + // AttributedPRHistoryFile is the store's file name under ReviewLinksDir. + AttributedPRHistoryFile = "attributed-pr-history.json" + attributedPRHistorySchemaVersion = 1 + + // Closed records whose PR was created longer ago than the retention are + // dropped when the store is written, and at most + // attributedPRHistoryMaxRecords closed records are kept (oldest created + // go first). Either prune moves the coverage start forward, so a window + // reaching past it reads as partial. + attributedPRHistoryRetention = 400 * 24 * time.Hour + attributedPRHistoryMaxRecords = 20000 + + // attributedPRHistoryScanStamp is how stale the persisted last-scan time + // may get before a scan alone rewrites the file. Continuity is judged + // against it, so a stale stamp can only make a gap look earlier, never + // hide one. + attributedPRHistoryScanStamp = time.Hour + // attributedPRHistoryOpenSeenRefresh is how often an open record's + // last-seen time is rewritten. Open records not seen within the retention + // (a close never observed, a repo no longer enumerated) are pruned. + attributedPRHistoryOpenSeenRefresh = 24 * time.Hour + + attributedPRBackfillLookbackEnv = "HIVE_ATTRIBUTED_PR_BACKFILL_LOOKBACK" + // attributedPRBackfillDefaultLookback is how far back (by PR creation) + // the one-time backfill walks: the longest bounded window the cards offer. + attributedPRBackfillDefaultLookback = 30 * 24 * time.Hour + // Per enumeration pass, across all repos: closed-PR list pages, and PRs + // whose rework is measured (about three calls each). Only spent while + // the API budget is normal. + attributedPRBackfillPagesPerPass = 2 + attributedPRBackfillReworkPerPass = 5 + // attributedPRBackfillReworkTries bounds rework retries for one PR, so a + // PR whose reviews cannot be read does not hold the backfill open. + attributedPRBackfillReworkTries = 3 + // attributedPRBackfillReworkTransientTries bounds retries after GitHub + // hiccups (5xx, network, timeouts) for one PR: more forgiving than a + // per-PR error, but finite, so a PR whose fetch keeps failing cannot + // hold the repo's backfill open forever. + attributedPRBackfillReworkTransientTries = 10 + // attributedPRBackfillListFailures bounds failed listings of one walk + // (per-repo errors such as 403/404/410/422, or repeated hiccups). A walk + // that hits it stops: the repo is never marked done, so its coverage + // keeps reporting that it does not reach the horizon. + attributedPRBackfillListFailures = 5 +) + +// AttributedPRHistoryPath is where the hive keeps its attributed PR history. +var AttributedPRHistoryPath = filepath.Join(ReviewLinksDir, AttributedPRHistoryFile) + +// AttributedPRCoverage says how far back the attributed PR history is +// complete across the client's active repos, in PR creation time: every +// attributed PR created at or after Since is held. +type AttributedPRCoverage struct { + // Available is false when no history store is configured; callers then + // fall back to the live scan's lookback. + Available bool + // Since is the coverage start; zero when Full. + Since time.Time + // Full means the history holds every attributed PR the repos ever had. + Full bool +} + +// attributedPRRecord is one stored PR: the attribution trailer's parsed +// values and the outcome and rework fields the PRs-by-model cards read. +type attributedPRRecord struct { + Repo string `json:"repo"` + Number int `json:"number"` + Title string `json:"title,omitempty"` + URL string `json:"url,omitempty"` + Labels []string `json:"labels,omitempty"` + Agent string `json:"agent,omitempty"` + Backend string `json:"backend,omitempty"` + Model string `json:"model,omitempty"` + Effort string `json:"effort,omitempty"` + State string `json:"state,omitempty"` + CreatedAt time.Time `json:"created_at,omitzero"` + ClosedAt time.Time `json:"closed_at,omitzero"` + MergedAt time.Time `json:"merged_at,omitzero"` + Supersedes int `json:"supersedes,omitempty"` + Rework PRReworkStats `json:"rework,omitzero"` + // ReworkKnown marks Rework as the closed PR's measurement (from the + // rework cache or a backfill fetch), not a zero left by a failed fetch. + ReworkKnown bool `json:"rework_known,omitempty"` + ReworkTries int `json:"rework_tries,omitempty"` + // ReworkTransientTries counts fetches lost to GitHub hiccups. + ReworkTransientTries int `json:"rework_transient_tries,omitempty"` + FirstSeenAt time.Time `json:"first_seen_at,omitzero"` + LastSeenAt time.Time `json:"last_seen_at,omitzero"` +} + +func (r *attributedPRRecord) closed() bool { + return !r.MergedAt.IsZero() || !r.ClosedAt.IsZero() || strings.EqualFold(r.State, "closed") +} + +func (r *attributedPRRecord) reworkPending() bool { + return r.closed() && !r.ReworkKnown && r.ReworkTries < attributedPRBackfillReworkTries && + r.ReworkTransientTries < attributedPRBackfillReworkTransientTries +} + +func (r *attributedPRRecord) pullRequest() PullRequest { + return PullRequest{ + Repo: r.Repo, + Number: r.Number, + Title: r.Title, + URL: r.URL, + Labels: append([]string(nil), r.Labels...), + CreatedAt: r.CreatedAt, + ClosedAt: r.ClosedAt, + MergedAt: r.MergedAt, + State: r.State, + HiveAttributed: true, + HiveAgent: r.Agent, + HiveBackend: r.Backend, + HiveModel: r.Model, + HiveEffort: r.Effort, + Supersedes: r.Supersedes, + Rework: r.Rework, + } +} + +func attributedPRRecordFrom(pr PullRequest) *attributedPRRecord { + return &attributedPRRecord{ + Repo: pr.Repo, + Number: pr.Number, + Title: pr.Title, + URL: pr.URL, + Labels: append([]string(nil), pr.Labels...), + Agent: pr.HiveAgent, + Backend: pr.HiveBackend, + Model: pr.HiveModel, + Effort: pr.HiveEffort, + State: pr.State, + CreatedAt: utcOrZero(pr.CreatedAt), + ClosedAt: utcOrZero(pr.ClosedAt), + MergedAt: utcOrZero(pr.MergedAt), + Supersedes: pr.Supersedes, + Rework: pr.Rework, + } +} + +func utcOrZero(t time.Time) time.Time { + if t.IsZero() { + return time.Time{} + } + return t.UTC() +} + +// attributedPRRepoState is one repo's scan continuity and backfill progress. +type attributedPRRepoState struct { + // LastScanAt is a live closed-PR scan's start time, at most + // attributedPRHistoryScanStamp older than the latest scan. + LastScanAt time.Time `json:"last_scan_at,omitzero"` + // RunStart is the first scan of the current unbroken run of scans (each + // reached back, by updated time, to the one before it). RunFloor is the + // updated-time floor that first scan reached; every closed PR created at + // or after it has been seen since. RunFull is set once a scan in the run + // listed the repo's whole closed-PR history. + RunStart time.Time `json:"run_start,omitzero"` + RunFloor time.Time `json:"run_floor,omitzero"` + RunFull bool `json:"run_full,omitempty"` + Backfill attributedPRBackfillState `json:"backfill"` +} + +// attributedPRBackfillState is the resumable cursor of the one-time backfill: +// closed PRs sorted by creation, newest first, down to Horizon. +type attributedPRBackfillState struct { + StartedAt time.Time `json:"started_at,omitzero"` + Horizon time.Time `json:"horizon,omitzero"` + NextPage int `json:"next_page,omitempty"` + // LastPage is the page read last. A pass that resumes reads it again + // first: a closed PR reopened since shifts the list forward, and the + // re-read catches up to a page's worth of PRs that moved behind the + // cursor (records are keyed, so the overlap only rewrites them). + LastPage int `json:"last_page,omitempty"` + // ListFailures counts this walk's failed listings since its last good + // page; at attributedPRBackfillListFailures the walk stops. + ListFailures int `json:"list_failures,omitempty"` + Listed bool `json:"listed,omitempty"` + ReachedEnd bool `json:"reached_end,omitempty"` + Done bool `json:"done,omitempty"` + CompletedAt time.Time `json:"completed_at,omitzero"` +} + +type attributedPRHistoryFile struct { + SchemaVersion int `json:"schema_version"` + GeneratedAt time.Time `json:"generated_at"` + // PrunedThrough: closed records created before it may have been + // dropped by retention, so coverage never starts earlier. + PrunedThrough time.Time `json:"pruned_through,omitzero"` + Repos map[string]*attributedPRRepoState `json:"repos"` + Records map[string]*attributedPRRecord `json:"records"` +} + +// attributedPRHistory is the in-memory store over one file. Every method is +// nil-safe and best-effort: a store problem is logged, never returned to +// enumeration. +type attributedPRHistory struct { + mu sync.Mutex + path string + logger *slog.Logger + file attributedPRHistoryFile + dirty bool + lastWarn string + // stepMu single-flights the backfill step: several callers run + // EnumerateActionable, and two steps on one cursor would list the same + // pages and measure the same PRs twice. + stepMu sync.Mutex + // enumerated holds the repos whose live PR enumeration has succeeded in + // this process. One of them with no scan state yet is uncovered (its + // closed-PR scan failed, or the store was reset); a configured repo that + // never enumerated has nothing live or stored and is left out. + enumerated map[string]bool +} + +// attributedPRHistories shares one store per path across clients, so a +// rebuilt App client and the one it replaces never write diverging copies. +var attributedPRHistories sync.Map + +func attributedPRHistoryAt(path string, logger *slog.Logger) *attributedPRHistory { + path = strings.TrimSpace(path) + if path == "" { + return nil + } + if v, ok := attributedPRHistories.Load(path); ok { + return v.(*attributedPRHistory) + } + v, _ := attributedPRHistories.LoadOrStore(path, loadAttributedPRHistory(path, logger)) + return v.(*attributedPRHistory) +} + +// loadAttributedPRHistory reads path. A missing file is an empty store; an +// unreadable, malformed or other-schema file is logged and treated as empty +// (the backfill then runs again). +func loadAttributedPRHistory(path string, logger *slog.Logger) *attributedPRHistory { + h := &attributedPRHistory{path: path, logger: logger} + h.file = attributedPRHistoryFile{SchemaVersion: attributedPRHistorySchemaVersion} + defer h.ensureMapsLocked() + raw, err := os.ReadFile(path) + if err != nil { + if !errors.Is(err, os.ErrNotExist) && logger != nil { + logger.Warn("attributed PR history: file unreadable; starting empty", "path", path, "error", err) + } + return h + } + var loaded attributedPRHistoryFile + if err := json.Unmarshal(raw, &loaded); err != nil || loaded.SchemaVersion != attributedPRHistorySchemaVersion { + if logger != nil { + logger.Warn("attributed PR history: file malformed or from another schema; starting empty", + "path", path, "schema_version", loaded.SchemaVersion, "error", err) + } + return h + } + // A null entry (a hand-edited or truncated file) would be a nil pointer + // in every walk below; drop it rather than panic inside enumeration. + for key, st := range loaded.Repos { + if st == nil { + delete(loaded.Repos, key) + } + } + for key, rec := range loaded.Records { + if rec == nil || strings.TrimSpace(rec.Repo) == "" { + delete(loaded.Records, key) + } + } + h.file = loaded + return h +} + +func (h *attributedPRHistory) ensureMapsLocked() { + if h.file.Repos == nil { + h.file.Repos = map[string]*attributedPRRepoState{} + } + if h.file.Records == nil { + h.file.Records = map[string]*attributedPRRecord{} + } +} + +func (h *attributedPRHistory) repoLocked(repo string) *attributedPRRepoState { + h.ensureMapsLocked() + st := h.file.Repos[repo] + if st == nil { + st = &attributedPRRepoState{} + h.file.Repos[repo] = st + } + return st +} + +// noteScan records a finished live closed-PR scan of repo that started at +// scanAt. floor is the oldest updated time the scan is complete back to; +// complete means it listed the repo's whole closed history. A scan whose floor +// is newer than the previous scan leaves a stretch nobody listed, so the run +// (and the coverage it carries) starts over. +func (h *attributedPRHistory) noteScan(repo string, scanAt, floor time.Time, complete bool) { + if h == nil { + return + } + h.mu.Lock() + defer h.mu.Unlock() + st := h.repoLocked(repo) + runChanged := false + switch { + case complete: + if st.RunStart.IsZero() { + st.RunStart = scanAt + } + runChanged = !st.RunFull + st.RunFull = true + case st.LastScanAt.IsZero() || floor.After(st.LastScanAt): + st.RunStart, st.RunFloor, st.RunFull = scanAt, floor, false + runChanged = true + } + if runChanged || st.LastScanAt.IsZero() || scanAt.Sub(st.LastScanAt) >= attributedPRHistoryScanStamp { + st.LastScanAt = scanAt + h.dirty = true + } +} + +// upsert stores the attributed PRs of one pass. A record that stays open is +// not rewritten (open PRs are served from the live listing); a closed one +// takes its rework from the rework cache, keeping a measured rework over a +// failed fetch's zero. +func (h *attributedPRHistory) upsert(prs []PullRequest, reworkCache map[string]PRReworkStats, now time.Time) { + if h == nil || len(prs) == 0 { + return + } + h.mu.Lock() + defer h.mu.Unlock() + h.ensureMapsLocked() + pruneFloor := h.pruneFloorLocked(now) + for _, pr := range prs { + if !pr.HiveAttributed || strings.TrimSpace(pr.Repo) == "" || pr.Number <= 0 { + continue + } + key := ReviewLinkKey(pr.Repo, pr.Number) + rec := attributedPRRecordFrom(pr) + // A closed PR older than what retention keeps would be pruned again + // on the next write: keeping it out saves a rewrite every pass. + if rec.closed() && !rec.CreatedAt.IsZero() && rec.CreatedAt.Before(pruneFloor) { + continue + } + if rec.closed() { + if cached, ok := reworkCache[key]; ok { + rec.Rework, rec.ReworkKnown = cached, true + } + } + old := h.file.Records[key] + if old == nil { + rec.FirstSeenAt, rec.LastSeenAt = now.UTC(), now.UTC() + h.file.Records[key] = rec + h.dirty = true + continue + } + if !old.closed() && !rec.closed() { + if now.Sub(old.LastSeenAt) >= attributedPRHistoryOpenSeenRefresh { + old.LastSeenAt = now.UTC() + h.dirty = true + } + continue + } + rec.FirstSeenAt, rec.LastSeenAt = old.FirstSeenAt, old.LastSeenAt + if !rec.ReworkKnown { + if old.ReworkKnown && rec.closed() { + rec.Rework, rec.ReworkKnown = old.Rework, true + } else { + rec.ReworkTries, rec.ReworkTransientTries = old.ReworkTries, old.ReworkTransientTries + } + } + if !sameAttributedPRRecord(old, rec) { + h.file.Records[key] = rec + h.dirty = true + } + } +} + +func sameAttributedPRRecord(a, b *attributedPRRecord) bool { + ja, errA := json.Marshal(a) + jb, errB := json.Marshal(b) + return errA == nil && errB == nil && bytes.Equal(ja, jb) +} + +// flush prunes and writes the store when anything changed. configured is the +// client's configured repos (nil: all count toward the record cap). +func (h *attributedPRHistory) flush(now time.Time, configured []string) error { + if h == nil { + return nil + } + h.mu.Lock() + defer h.mu.Unlock() + if !h.dirty { + return nil + } + h.pruneLocked(now, configured) + h.file.SchemaVersion = attributedPRHistorySchemaVersion + h.file.GeneratedAt = now.UTC() + raw, err := json.Marshal(h.file) + if err != nil { + return fmt.Errorf("encoding attributed PR history: %w", err) + } + if err := os.MkdirAll(filepath.Dir(h.path), 0o755); err != nil { + return fmt.Errorf("creating attributed PR history directory: %w", err) + } + tmp := h.path + ".tmp" + if err := os.WriteFile(tmp, raw, 0o644); err != nil { + return fmt.Errorf("writing attributed PR history: %w", err) + } + if err := os.Rename(tmp, h.path); err != nil { + _ = os.Remove(tmp) + return fmt.Errorf("replacing attributed PR history: %w", err) + } + h.dirty = false + return nil +} + +// pruneFloorLocked is the creation time before which closed records are not +// kept: the retention cutoff, or PrunedThrough when the cap pruned past it. +func (h *attributedPRHistory) pruneFloorLocked(now time.Time) time.Time { + cutoff := now.Add(-attributedPRHistoryRetention).UTC() + if h.file.PrunedThrough.After(cutoff) { + return h.file.PrunedThrough + } + return cutoff +} + +// pruneLocked drops closed records past the retention and open records not +// seen within it, then the oldest closed records of configured repos beyond +// the cap, raising PrunedThrough to cover the closed ones that went. Records +// of repos no longer configured are outside the cap (they are never served) +// and age out by the retention alone. +func (h *attributedPRHistory) pruneLocked(now time.Time, configured []string) { + cutoff := now.Add(-attributedPRHistoryRetention).UTC() + var want map[string]bool + if configured != nil { + want = make(map[string]bool, len(configured)) + for _, repo := range configured { + want[repo] = true + } + } + var closed []string + for key, rec := range h.file.Records { + if !rec.closed() { + seen := rec.LastSeenAt + if seen.IsZero() { + seen = rec.FirstSeenAt + } + if !seen.IsZero() && seen.Before(cutoff) { + delete(h.file.Records, key) + } + continue + } + if !rec.CreatedAt.IsZero() && rec.CreatedAt.Before(cutoff) { + delete(h.file.Records, key) + if cutoff.After(h.file.PrunedThrough) { + h.file.PrunedThrough = cutoff + } + continue + } + if want == nil || want[rec.Repo] { + closed = append(closed, key) + } + } + if len(closed) <= attributedPRHistoryMaxRecords { + return + } + sort.Slice(closed, func(i, j int) bool { + a, b := h.file.Records[closed[i]], h.file.Records[closed[j]] + if !a.CreatedAt.Equal(b.CreatedAt) { + return a.CreatedAt.Before(b.CreatedAt) + } + return closed[i] < closed[j] + }) + for _, key := range closed[:len(closed)-attributedPRHistoryMaxRecords] { + if through := h.file.Records[key].CreatedAt.Add(time.Nanosecond); through.After(h.file.PrunedThrough) { + h.file.PrunedThrough = through + } + delete(h.file.Records, key) + } +} + +// coveredSinceLocked is one repo's coverage start. ok is false before the +// repo's first live scan. A repo whose last scan is older than the live +// scan's lookback is uncovered (since = now): PRs closed after that scan are +// neither stored nor in the live data, and the next scan cannot reach back +// to all of them. +func (h *attributedPRHistory) coveredSinceLocked(repo string, now time.Time, liveLookback time.Duration) (since time.Time, full, ok bool) { + st := h.file.Repos[repo] + if st == nil || st.LastScanAt.IsZero() { + return time.Time{}, false, false + } + if now.Sub(st.LastScanAt) > liveLookback { + return now, false, true + } + if st.RunFull { + return time.Time{}, true, true + } + since = st.RunFloor + // The backfill's horizon counts only while the run of live scans that + // was under way when it started is unbroken: those scans saw every PR + // that closed after the walk passed its place in the list. + if bf := st.Backfill; bf.Done && !bf.StartedAt.IsZero() && !st.RunStart.After(bf.StartedAt) { + if bf.ReachedEnd { + return time.Time{}, true, true + } + if bf.Horizon.Before(since) { + since = bf.Horizon + } + } + return since, false, true +} + +// snapshot returns the closed records of repos as PullRequests and the +// coverage across repos. +func (h *attributedPRHistory) snapshot(repos []string, now time.Time) ([]PullRequest, AttributedPRCoverage) { + if h == nil || len(repos) == 0 { + return nil, AttributedPRCoverage{} + } + h.mu.Lock() + defer h.mu.Unlock() + h.ensureMapsLocked() + cov := AttributedPRCoverage{Available: true, Full: true} + want := make(map[string]bool, len(repos)) + scanned := 0 + liveLookback := attributedClosedPRLookback() + for _, repo := range repos { + want[repo] = true + since, full, ok := h.coveredSinceLocked(repo, now, liveLookback) + if !ok { + if !h.enumerated[repo] { + // Never enumerated in this process (no access, deleted, or + // not reached yet): nothing of it is live or stored, so it + // cannot make a window partial. + continue + } + // Enumerated but never scanned: its open PRs are live while + // its closed ones are missing, so nothing is covered. + since, full = now, false + } + scanned++ + if !full { + if cov.Full || since.After(cov.Since) { + cov.Since = since + } + cov.Full = false + } + } + if scanned == 0 { + cov.Full, cov.Since = false, now + } + if !h.file.PrunedThrough.IsZero() { + if cov.Full || h.file.PrunedThrough.After(cov.Since) { + cov.Since = h.file.PrunedThrough + } + cov.Full = false + } + if cov.Full { + cov.Since = time.Time{} + } + var out []PullRequest + for _, rec := range h.file.Records { + if want[rec.Repo] && rec.closed() { + out = append(out, rec.pullRequest()) + } + } + sort.Slice(out, func(i, j int) bool { + if out[i].Repo != out[j].Repo { + return out[i].Repo < out[j].Repo + } + return out[i].Number < out[j].Number + }) + return out, cov +} + +// noteEnumerated records that repo's live PR enumeration succeeded. +func (h *attributedPRHistory) noteEnumerated(repo string) { + if h == nil { + return + } + h.mu.Lock() + defer h.mu.Unlock() + if h.enumerated == nil { + h.enumerated = map[string]bool{} + } + h.enumerated[repo] = true +} + +// backfillStart returns repo's backfill cursor, starting (or restarting) it +// when needed. ok is false when the repo needs no backfill: done, or no live +// scan yet to anchor it. +func (h *attributedPRHistory) backfillStart(repo string, now time.Time, lookback time.Duration) (attributedPRBackfillState, bool) { + h.mu.Lock() + defer h.mu.Unlock() + st := h.repoLocked(repo) + bf := &st.Backfill + if bf.Done || st.LastScanAt.IsZero() { + return *bf, false + } + if st.RunFull { + // The live scan already listed the whole closed history; only the + // rework measurements remain. + if !bf.Listed { + *bf = attributedPRBackfillState{StartedAt: now.UTC(), Listed: true, ReachedEnd: true} + h.dirty = true + } + return *bf, true + } + // A gap in the live scans after the walk began means PRs that closed in + // the gap may sit behind the cursor; walk again from the top. + if bf.StartedAt.IsZero() || st.RunStart.After(bf.StartedAt) { + *bf = attributedPRBackfillState{StartedAt: now.UTC(), Horizon: now.Add(-lookback).UTC(), NextPage: 1} + h.dirty = true + } + if !bf.Listed && bf.ListFailures >= attributedPRBackfillListFailures { + // The walk gave up on this repo; it stays not done. + return *bf, false + } + return *bf, true +} + +// backfillListFailed counts one failed listing of repo's walk. +func (h *attributedPRHistory) backfillListFailed(repo string) { + h.mu.Lock() + defer h.mu.Unlock() + h.repoLocked(repo).Backfill.ListFailures++ + h.dirty = true +} + +// backfillPage stores one listed page and advances the cursor. +func (h *attributedPRHistory) backfillPage(repo string, prs []PullRequest, reworkCache map[string]PRReworkStats, page, nextPage int, reachedHorizon bool, now time.Time) { + h.upsert(prs, reworkCache, now) + h.mu.Lock() + defer h.mu.Unlock() + bf := &h.repoLocked(repo).Backfill + bf.LastPage, bf.NextPage, bf.ListFailures = page, nextPage, 0 + if reachedHorizon || nextPage == 0 { + bf.Listed = true + bf.ReachedEnd = !reachedHorizon + } + h.dirty = true +} + +// reworkPending lists repo's closed records still waiting for a rework +// measurement, merged first, newest first. +func (h *attributedPRHistory) reworkPending(repo string) []PullRequest { + h.mu.Lock() + defer h.mu.Unlock() + var out []PullRequest + for _, rec := range h.file.Records { + if rec.Repo == repo && rec.reworkPending() { + out = append(out, rec.pullRequest()) + } + } + sort.Slice(out, func(i, j int) bool { + mi, mj := !out[i].MergedAt.IsZero(), !out[j].MergedAt.IsZero() + if mi != mj { + return mi + } + if !out[i].CreatedAt.Equal(out[j].CreatedAt) { + return out[i].CreatedAt.After(out[j].CreatedAt) + } + return out[i].Number > out[j].Number + }) + return out +} + +// setRework records a rework measurement (ok) or one more failed try. +func (h *attributedPRHistory) setRework(key string, rework PRReworkStats, ok bool) { + h.mu.Lock() + defer h.mu.Unlock() + rec := h.file.Records[key] + if rec == nil { + return + } + if ok { + rec.Rework, rec.ReworkKnown = rework, true + } else { + rec.ReworkTries++ + } + h.dirty = true +} + +// reworkTransientFailed counts one rework fetch lost to a GitHub hiccup. +func (h *attributedPRHistory) reworkTransientFailed(key string) { + h.mu.Lock() + defer h.mu.Unlock() + if rec := h.file.Records[key]; rec != nil { + rec.ReworkTransientTries++ + h.dirty = true + } +} + +// backfillFinish marks repo's backfill complete once the walk reached its +// horizon and no closed record still waits for rework. +func (h *attributedPRHistory) backfillFinish(repo string, now time.Time) { + h.mu.Lock() + defer h.mu.Unlock() + bf := &h.repoLocked(repo).Backfill + if bf.Done || !bf.Listed { + return + } + for _, rec := range h.file.Records { + if rec.Repo == repo && rec.reworkPending() { + return + } + } + bf.Done, bf.CompletedAt = true, now.UTC() + h.dirty = true +} + +// warn logs a store problem once per distinct message. +func (h *attributedPRHistory) warn(msg string, err error, args ...any) { + if h == nil || err == nil { + return + } + h.mu.Lock() + repeat := h.lastWarn == err.Error() + h.lastWarn = err.Error() + h.mu.Unlock() + if repeat || h.logger == nil { + return + } + h.logger.Warn(msg, append(args, "path", h.path, "error", err)...) +} + +// SetAttributedPRHistoryPath persists the attributed PR history at path +// (loaded now): the closed PRs behind the PRs-by-model cards' 30d and All +// windows survive the bounded live scan and a restart, and the one-time +// backfill runs from it. Empty disables the store; the cards then rest on the +// live scan alone, as before. +func (c *Client) SetAttributedPRHistoryPath(path string) { + if c == nil { + return + } + c.attributedHistory.Store(attributedPRHistoryAt(path, c.logger)) +} + +func (c *Client) attributedPRHistory() *attributedPRHistory { + if c == nil { + return nil + } + return c.attributedHistory.Load() +} + +// AttributedPRHistory returns the stored closed attributed PRs of the active +// repos and how far back they are complete. Coverage.Available is false when +// no store is configured. Callers merge these under the live enumeration's +// records, which carry the freshest state. +func (c *Client) AttributedPRHistory() ([]PullRequest, AttributedPRCoverage) { + return c.attributedPRHistory().snapshot(c.activeRepos(), time.Now()) +} + +// recordAttributedPRHistory marks repo enumerated and stores its attributed +// PRs from this pass (after rework enrichment, so closed records carry their +// measurement). reworkCache is the cache the enrichment just loaded and +// updated, so it is not read from disk twice. The write happens once per +// pass in syncAttributedPRHistory. +func (c *Client) recordAttributedPRHistory(repo string, prs []PullRequest, reworkCache map[string]PRReworkStats) { + h := c.attributedPRHistory() + if h == nil { + return + } + // Called only once the repo's open-PR listing succeeded. + h.noteEnumerated(repo) + if len(prs) == 0 { + return + } + if reworkCache == nil { + reworkCache = loadPRReworkCache() + } + h.upsert(prs, reworkCache, time.Now()) +} + +// syncAttributedPRHistory runs the enumeration pass's backfill step and +// writes the store. Errors are logged, never returned: the history is a +// dashboard aid and must not fail enumeration. Only one backfill step runs at +// a time; a pass that finds another step running skips its own. +func (c *Client) syncAttributedPRHistory(ctx context.Context, repos []string) { + h := c.attributedPRHistory() + if h == nil { + return + } + now := time.Now() + if h.stepMu.TryLock() { + c.backfillAttributedPRHistory(ctx, h, repos, now) + h.stepMu.Unlock() + } + if err := h.flush(now, c.getRepos()); err != nil { + h.warn("attributed PR history: write failed; keeping it in memory and retrying next pass", err) + } +} + +func attributedPRBackfillLookback() time.Duration { + return lookbackFromEnv(attributedPRBackfillLookbackEnv, attributedPRBackfillDefaultLookback) +} + +// attributedPRBackfillAllowed is the backfill's rate-limit gate: it spends +// calls only while the API budget is normal, leaving conserve and critical +// headroom to the work that unsticks items. +func (c *Client) attributedPRBackfillAllowed(ctx context.Context) bool { + if ctx.Err() != nil { + return false + } + mode, _ := c.APIBudgetMode() + return mode == APIBudgetNormal +} + +// attributedPRBackfillErrClass sorts a backfill call's error by what it says +// about the PR or repo asked for. +type attributedPRBackfillErrClass int + +const ( + // attributedPRBackfillErrItem is about this PR or repo (404, 403, a + // malformed answer): a rework measurement counts one try. + attributedPRBackfillErrItem attributedPRBackfillErrClass = iota + // attributedPRBackfillErrTransient is a GitHub hiccup (5xx, network, a + // request timeout): no try is counted and the pass moves to the next repo. + attributedPRBackfillErrTransient + // attributedPRBackfillErrStop is the budget or the pass itself (rate + // limit, pacing shed, enumeration cancelled or out of time): the pass + // ends and nothing is counted against the PR. + attributedPRBackfillErrStop +) + +func classifyAttributedPRBackfillErr(ctx context.Context, err error) attributedPRBackfillErrClass { + if ctx.Err() != nil || errors.Is(err, context.Canceled) || errors.Is(err, ErrPacingShed) || + strings.Contains(err.Error(), pacingErrPrefix) || isRateLimitErr(err) { + return attributedPRBackfillErrStop + } + var ghErr *gh.ErrorResponse + if errors.As(err, &ghErr) && ghErr.Response != nil && ghErr.Response.StatusCode == http.StatusTooManyRequests { + return attributedPRBackfillErrStop + } + if isRetryableGitHubError(err) { + return attributedPRBackfillErrTransient + } + return attributedPRBackfillErrItem +} + +// backfillAttributedPRHistory is one pass of the one-time backfill: at most +// attributedPRBackfillPagesPerPass closed-PR pages and +// attributedPRBackfillReworkPerPass rework measurements across all repos, +// resuming from each repo's saved cursor. A repo whose backfill is done costs +// nothing, so once every repo is done a pass makes no GitHub call. +func (c *Client) backfillAttributedPRHistory(ctx context.Context, h *attributedPRHistory, repos []string, now time.Time) { + if h == nil || len(repos) == 0 { + return + } + ctx = WithPacingClass(ctx, PacingLow) + lookback := attributedPRBackfillLookback() + pages, reworks := attributedPRBackfillPagesPerPass, attributedPRBackfillReworkPerPass + var reworkCache map[string]PRReworkStats + reworkCacheChanged := false + // Like enrichAttributedPRRework, this writes back the whole cache map it + // loaded, so a concurrent enrichment's additions in between are lost + // (last writer wins) and simply measured again later. + defer func() { + if reworkCacheChanged { + storePRReworkCache(reworkCache) + } + }() + for _, repo := range repos { + if pages <= 0 && reworks <= 0 { + return + } + bf, ok := h.backfillStart(repo, now, lookback) + if !ok { + continue + } + if reworkCache == nil { + reworkCache = loadPRReworkCache() + } + stopped := false + if !bf.Listed && pages > 0 { + pages, stopped = c.backfillAttributedPRPages(ctx, h, repo, bf, pages, reworkCache, now) + } + if !stopped && reworks > 0 { + var changed bool + reworks, stopped, changed = c.backfillAttributedPRRework(ctx, h, repo, reworks, reworkCache) + reworkCacheChanged = reworkCacheChanged || changed + } + h.backfillFinish(repo, now) + if stopped { + return + } + } +} + +// backfillAttributedPRPages lists closed PRs by creation, newest first, from +// the cursor until the page budget runs out or the walk passes the horizon. +// A resumed walk first re-reads the page it read last (see LastPage). stopped +// reports that the pass should end (budget gate, rate limit, cancellation); +// any other listing error leaves this repo's cursor for the next pass and lets +// the pass go on to the next repo. +// +// What the re-read cannot catch: more than a page of closed PRs reopened +// between two passes of one walk, which could shift a PR behind the cursor +// unseen. Reopening is rare and a walk takes few passes, so this is accepted. +func (c *Client) backfillAttributedPRPages(ctx context.Context, h *attributedPRHistory, repo string, bf attributedPRBackfillState, budget int, reworkCache map[string]PRReworkStats, now time.Time) (int, bool) { + owner, repoName := c.splitRepo(repo) + page := bf.NextPage + if page <= 0 { + page = 1 + } + if bf.LastPage > 0 && bf.LastPage < page && budget > 1 { + page = bf.LastPage + } + for budget > 0 { + if !c.attributedPRBackfillAllowed(ctx) { + return budget, true + } + opts := &gh.PullRequestListOptions{ + State: "closed", + Sort: "created", + Direction: "desc", + ListOptions: gh.ListOptions{PerPage: 100, Page: page}, + } + budget-- + prs, resp, err := c.client.PullRequests.List(ctx, owner, repoName, opts) + if err != nil { + h.warn("attributed PR history: backfill listing failed; retrying next pass", fmt.Errorf("listing closed PRs for %s/%s: %w", owner, repoName, err), "repo", repo) + if classifyAttributedPRBackfillErr(ctx, err) == attributedPRBackfillErrStop { + return budget, true + } + // A per-repo error or a hiccup: counted against the walk, which + // gives up after attributedPRBackfillListFailures in a row. + h.backfillListFailed(repo) + return budget, false + } + var recs []PullRequest + reached := false + for _, pr := range prs { + if pr == nil { + continue + } + if pr.GetCreatedAt().Time.Before(bf.Horizon) { + reached = true + break + } + if meta, ok := ParseAttributionTrailer(pr.GetBody()); ok { + recs = append(recs, pullRequestAttributionRecord(repo, pr, meta)) + } + } + next := 0 + if resp != nil { + next = resp.NextPage + } + h.backfillPage(repo, recs, reworkCache, page, next, reached, now) + if reached || next == 0 { + return budget, false + } + page = next + } + return budget, false +} + +// backfillAttributedPRRework measures rework for repo's closed records that +// lack it, through the same fetch and cache the live enrichment uses. An +// error about the PR itself counts one of its tries and a hiccup one of its +// (more numerous) transient tries, and the next PR gets its turn; a budget or +// cancellation error ends the pass without counting anything. +func (c *Client) backfillAttributedPRRework(ctx context.Context, h *attributedPRHistory, repo string, budget int, reworkCache map[string]PRReworkStats) (left int, stopped, cacheChanged bool) { + for _, pr := range h.reworkPending(repo) { + key := ReviewLinkKey(pr.Repo, pr.Number) + if cached, ok := reworkCache[key]; ok { + h.setRework(key, cached, true) + continue + } + if budget <= 0 { + return 0, false, cacheChanged + } + if !c.attributedPRBackfillAllowed(ctx) { + return budget, true, cacheChanged + } + budget-- + rework, err := c.fetchPRRework(ctx, pr) + if err != nil { + switch classifyAttributedPRBackfillErr(ctx, err) { + case attributedPRBackfillErrStop: + return budget, true, cacheChanged + case attributedPRBackfillErrTransient: + // Counted on its own, larger cap, and the next PR gets its + // turn: the pending order is fixed, so returning here would + // put the same failing PR first on every pass. + h.reworkTransientFailed(key) + continue + } + h.setRework(key, PRReworkStats{}, false) + continue + } + reworkCache[key] = rework + cacheChanged = true + h.setRework(key, rework, true) + } + return budget, false, cacheChanged +} diff --git a/src/pkg/github/attributed_pr_history_test.go b/src/pkg/github/attributed_pr_history_test.go new file mode 100644 index 0000000000..c1bb415cce --- /dev/null +++ b/src/pkg/github/attributed_pr_history_test.go @@ -0,0 +1,620 @@ +package github + +import ( + "context" + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strconv" + "strings" + "sync" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" +) + +func historyTestLogger() *slog.Logger { + return slog.New(slog.NewTextHandler(io.Discard, nil)) +} + +// useTempReworkCache points the rework cache at a scratch file for the test. +func useTempReworkCache(t *testing.T) { + t.Helper() + old := PRReworkCachePath + PRReworkCachePath = filepath.Join(t.TempDir(), PRReworkCacheFile) + t.Cleanup(func() { PRReworkCachePath = old }) +} + +// useHistoryAt gives c a fresh store read from path (not the process-wide +// registry), which is what a restart does. +func useHistoryAt(c *Client, path string) *attributedPRHistory { + h := loadAttributedPRHistory(path, historyTestLogger()) + c.attributedHistory.Store(h) + return h +} + +func historyNumbers(prs []PullRequest) []int { + var out []int + for _, pr := range prs { + out = append(out, pr.Number) + } + return out +} + +// A PR closed 20 days ago drops out of the bounded live scan; the store keeps +// it, and a restart (reload from disk) still has it. +func TestAttributedPRHistoryKeepsClosedPRPastLiveScanAndRestart(t *testing.T) { + useTempReworkCache(t) + now := time.Now().UTC() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasPrefix(r.URL.Path, "/repos/o/r/pulls/") || strings.HasPrefix(r.URL.Path, "/repos/o/r/issues/") { + writeJSON(t, w, []map[string]any{}) + return + } + if r.URL.Path != "/repos/o/r/pulls" { + t.Fatalf("unexpected request: %s?%s", r.URL.Path, r.URL.RawQuery) + } + switch r.URL.Query().Get("state") { + case "open": + writeJSON(t, w, []map[string]any{}) + case "closed": + pr := attributedClosedPR(7, now.Add(-20*24*time.Hour)) + pr["created_at"] = now.Add(-22 * 24 * time.Hour).Format(time.RFC3339) + pr["merged_at"] = now.Add(-20 * 24 * time.Hour).Format(time.RFC3339) + writeJSON(t, w, []map[string]any{pr}) + default: + t.Fatalf("unexpected state query: %q", r.URL.Query().Get("state")) + } + })) + defer srv.Close() + + path := filepath.Join(t.TempDir(), AttributedPRHistoryFile) + c := NewClientForTest(srv.URL, "o", []string{"r"}, historyTestLogger()) + useHistoryAt(c, path) + + // A 30-day lookback lists the PR once... + t.Setenv(attributedClosedPRLookbackEnv, "30d") + _, _, _, _, live, _, _, err := c.fetchPRs(context.Background(), "r", nil) + if err != nil || len(live) != 1 { + t.Fatalf("first fetchPRs: attributed=%v err=%v, want #7 listed", historyNumbers(live), err) + } + // ...the default 14-day scan no longer does. + t.Setenv(attributedClosedPRLookbackEnv, "14d") + _, _, _, _, attributed, _, _, err := c.fetchPRs(context.Background(), "r", nil) + if err != nil { + t.Fatalf("second fetchPRs: %v", err) + } + if len(attributed) != 0 { + t.Fatalf("live attributed = %v, want the 14-day scan to drop #7", historyNumbers(attributed)) + } + stored, _ := c.AttributedPRHistory() + // The stored attribution is what the live scan parsed (the trailer's + // model=auto normalizes like any other model value). + if len(stored) != 1 || stored[0].Number != 7 || stored[0].MergedAt.IsZero() || + stored[0].HiveAgent != live[0].HiveAgent || stored[0].HiveBackend != live[0].HiveBackend || stored[0].HiveModel != live[0].HiveModel || stored[0].HiveAgent == "" { + t.Fatalf("stored = %+v, want merged #7 with the live scan's attribution %+v", stored, live[0]) + } + c.syncAttributedPRHistory(context.Background(), []string{"r"}) + + raw, err := os.ReadFile(path) + if err != nil { + t.Fatalf("history file not written: %v", err) + } + if strings.Contains(string(raw), "-- hive:") || strings.Contains(string(raw), `"body"`) { + t.Fatalf("history file holds a PR body: %s", raw) + } + + restarted := NewClientForTest(srv.URL, "o", []string{"r"}, historyTestLogger()) + useHistoryAt(restarted, path) + stored, _ = restarted.AttributedPRHistory() + if len(stored) != 1 || stored[0].Number != 7 || stored[0].HiveBackend != "bob" { + t.Fatalf("after restart stored = %+v, want #7", stored) + } +} + +// A store that cannot be written only warns: enumeration still returns the +// repo's attributed PRs. +func TestAttributedPRHistoryWriteErrorDoesNotFailEnumeration(t *testing.T) { + useTempReworkCache(t) + mux := http.NewServeMux() + mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("state") == "closed" { + writeJSON(t, w, []map[string]any{attributedClosedPR(7, time.Now().Add(-time.Hour))}) + return + } + writeJSON(t, w, []map[string]any{}) + }) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, []map[string]any{}) + }) + srv := httptest.NewServer(mux) + defer srv.Close() + + // The store's directory is a regular file, so every write fails. + blocker := filepath.Join(t.TempDir(), "not-a-dir") + if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + c := newTestClient(t, srv, "o", []string{"r"}) + useHistoryAt(c, filepath.Join(blocker, AttributedPRHistoryFile)) + + result, err := c.EnumerateActionable(context.Background()) + if err != nil { + t.Fatalf("a history write error failed enumeration: %v", err) + } + if got := historyNumbers(result.PRs.Attributed); len(got) != 1 || got[0] != 7 { + t.Fatalf("attributed = %v, want #7", got) + } + if err := c.attributedPRHistory().flush(time.Now(), nil); err == nil { + t.Fatal("flush into a file-as-directory succeeded; the test did not exercise a write error") + } +} + +// attributedBackfillFake serves a closed-PR list sorted by creation over +// len(pages) pages, plus empty rework listings, and counts every request. +type attributedBackfillFake struct { + mu sync.Mutex + listPages []string + rework int + other []string +} + +func (b *attributedBackfillFake) handler(t *testing.T, pages [][]map[string]any) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + b.mu.Lock() + defer b.mu.Unlock() + switch { + case r.URL.Path == "/repos/o/r/pulls" && r.URL.Query().Get("state") == "closed" && r.URL.Query().Get("sort") == "created": + if got := r.URL.Query().Get("direction"); got != "desc" { + t.Errorf("backfill direction = %q, want desc", got) + } + page := r.URL.Query().Get("page") + if page == "" { + page = "1" + } + b.listPages = append(b.listPages, page) + n, _ := strconv.Atoi(page) + if n < 1 || n > len(pages) { + t.Errorf("backfill asked for page %q of %d", page, len(pages)) + writeJSON(t, w, []map[string]any{}) + return + } + if n < len(pages) { + w.Header().Set("Link", `<`+srvURL(r)+`/repos/o/r/pulls?state=closed&sort=created&direction=desc&page=`+strconv.Itoa(n+1)+`>; rel="next"`) + } + writeJSON(t, w, pages[n-1]) + case strings.HasPrefix(r.URL.Path, "/repos/o/r/pulls/") || strings.HasPrefix(r.URL.Path, "/repos/o/r/issues/"): + b.rework++ + writeJSON(t, w, []map[string]any{}) + default: + b.other = append(b.other, r.URL.Path+"?"+r.URL.RawQuery) + writeJSON(t, w, []map[string]any{}) + } + } +} + +func (b *attributedBackfillFake) take() (pages []string, rework int, other []string) { + b.mu.Lock() + defer b.mu.Unlock() + pages, rework, other = b.listPages, b.rework, b.other + b.listPages, b.rework, b.other = nil, 0, nil + return pages, rework, other +} + +func backfillMergedPR(number int, created time.Time) map[string]any { + pr := attributedClosedPR(number, created.Add(time.Hour)) + pr["created_at"] = created.Format(time.RFC3339) + pr["merged_at"] = created.Add(time.Hour).Format(time.RFC3339) + return pr +} + +// The backfill spends at most its per-pass page budget, resumes from the +// saved cursor after a restart, stops at the horizon, marks itself complete +// and then makes no calls at all. +func TestAttributedPRBackfillBudgetResumeAndComplete(t *testing.T) { + useTempReworkCache(t) + now := time.Now().UTC() + // No rate-limit evidence from other tests may gate the backfill. + resetRESTAccountingForTest(t, now) + day := 24 * time.Hour + pages := [][]map[string]any{ + {backfillMergedPR(30, now.Add(-16*day))}, + {backfillMergedPR(20, now.Add(-20*day))}, + {backfillMergedPR(10, now.Add(-25*day)), backfillMergedPR(5, now.Add(-45*day))}, + } + fake := &attributedBackfillFake{} + srv := httptest.NewServer(fake.handler(t, pages)) + defer srv.Close() + + path := filepath.Join(t.TempDir(), AttributedPRHistoryFile) + c := NewClientForTest(srv.URL, "o", []string{"r"}, historyTestLogger()) + h := useHistoryAt(c, path) + // The live scan has run once (14-day floor): the backfill's anchor. + h.noteScan("r", now, now.Add(-14*day), false) + + c.syncAttributedPRHistory(context.Background(), []string{"r"}) + gotPages, rework, other := fake.take() + if strings.Join(gotPages, ",") != "1,2" { + t.Fatalf("pass 1 listed pages %v, want the per-pass budget of %d pages (1,2)", gotPages, attributedPRBackfillPagesPerPass) + } + if rework == 0 || len(other) != 0 { + t.Fatalf("pass 1 rework calls=%d other=%v, want rework measured and nothing else", rework, other) + } + + // Restart: the cursor comes back from disk. + h = useHistoryAt(c, path) + if st := h.file.Repos["r"]; st == nil || st.Backfill.NextPage != 3 || st.Backfill.LastPage != 2 || st.Backfill.Listed || st.Backfill.Done { + t.Fatalf("persisted backfill = %+v, want cursor at page 3 after page 2, not listed", st) + } + c.syncAttributedPRHistory(context.Background(), []string{"r"}) + gotPages, _, other = fake.take() + // The resumed walk re-reads the page it read last, then goes on. + if strings.Join(gotPages, ",") != "2,3" || len(other) != 0 { + t.Fatalf("pass 2 listed pages %v other=%v, want page 2 re-read, then page 3", gotPages, other) + } + st := h.file.Repos["r"] + if !st.Backfill.Listed || st.Backfill.ReachedEnd || !st.Backfill.Done { + t.Fatalf("backfill after pass 2 = %+v, want listed to the horizon and done", st.Backfill) + } + stored, cov := c.AttributedPRHistory() + if got := historyNumbers(stored); len(got) != 3 || got[0] != 10 || got[1] != 20 || got[2] != 30 { + t.Fatalf("stored = %v, want #10 #20 #30 (#5 is past the horizon)", got) + } + for _, pr := range stored { + if pr.Rework.TimeToMergeMinutes != 60 { + t.Fatalf("stored #%d rework = %+v, want it measured", pr.Number, pr.Rework) + } + } + if !cov.Available || cov.Full || cov.Since.After(now.Add(-29*day)) { + t.Fatalf("coverage = %+v, want it to reach the 30-day horizon", cov) + } + + for i := 0; i < 3; i++ { + c.syncAttributedPRHistory(context.Background(), []string{"r"}) + } + if gotPages, rework, other := fake.take(); len(gotPages) != 0 || rework != 0 || len(other) != 0 { + t.Fatalf("finished backfill made calls: pages=%v rework=%d other=%v", gotPages, rework, other) + } +} + +// Outside the normal API budget the backfill makes no call and leaves its +// cursor where it was. +func TestAttributedPRBackfillRespectsAPIBudget(t *testing.T) { + useTempReworkCache(t) + now := time.Now().UTC() + resetRESTAccountingForTest(t, now) + fake := &attributedBackfillFake{} + srv := httptest.NewServer(fake.handler(t, [][]map[string]any{{backfillMergedPR(1, now.Add(-20*24*time.Hour))}})) + defer srv.Close() + + c := NewClientForTest(srv.URL, "o", []string{"r"}, historyTestLogger()) + c.rateLimits.now = func() time.Time { return now } + c.rateLimits.mu.Lock() + if c.rateLimits.windows == nil { + c.rateLimits.windows = map[string]rateLimitWindow{} + } + c.rateLimits.windows[githubCoreRateLimitResource] = rateLimitWindow{limit: 5000, remaining: 500, reset: now.Add(time.Hour), observedAt: now} + c.rateLimits.mu.Unlock() + if mode, _ := c.APIBudgetMode(); mode == APIBudgetNormal { + t.Fatal("test setup: API budget mode is normal") + } + h := useHistoryAt(c, filepath.Join(t.TempDir(), AttributedPRHistoryFile)) + h.noteScan("r", now, now.Add(-14*24*time.Hour), false) + + c.syncAttributedPRHistory(context.Background(), []string{"r"}) + if gotPages, rework, other := fake.take(); len(gotPages) != 0 || rework != 0 || len(other) != 0 { + t.Fatalf("backfill spent calls outside the normal budget: pages=%v rework=%d other=%v", gotPages, rework, other) + } + if bf := h.file.Repos["r"].Backfill; bf.NextPage != 1 || bf.Listed || bf.Done { + t.Fatalf("backfill = %+v, want the cursor still at page 1", bf) + } +} + +// Coverage follows the run of live scans: the first scan's floor, the +// backfill horizon once done, and a fresh floor after a gap nobody listed. +func TestAttributedPRHistoryCoverageTracksScanContinuity(t *testing.T) { + now := time.Now().UTC() + day := 24 * time.Hour + h := loadAttributedPRHistory(filepath.Join(t.TempDir(), AttributedPRHistoryFile), historyTestLogger()) + if _, cov := h.snapshot([]string{"r"}, now); !cov.Available || cov.Full || !cov.Since.Equal(now) { + t.Fatalf("coverage before any scan = %+v, want nothing covered", cov) + } + h.noteScan("r", now.Add(-2*day), now.Add(-16*day), false) + h.noteScan("r", now.Add(-day), now.Add(-15*day), false) + if _, cov := h.snapshot([]string{"r"}, now); !cov.Since.Equal(now.Add(-16 * day)) { + t.Fatalf("coverage = %+v, want the run's first floor", cov) + } + st := h.file.Repos["r"] + st.Backfill = attributedPRBackfillState{StartedAt: now.Add(-day), Horizon: now.Add(-31 * day), Listed: true, Done: true} + if _, cov := h.snapshot([]string{"r"}, now); !cov.Since.Equal(now.Add(-31 * day)) { + t.Fatalf("coverage = %+v, want the backfill horizon", cov) + } + // Twenty days with no scan: the next floor is past the last scan. + later := now.Add(20 * day) + h.noteScan("r", later, later.Add(-14*day), false) + if _, cov := h.snapshot([]string{"r"}, later); !cov.Since.Equal(later.Add(-14 * day)) { + t.Fatalf("coverage after a gap = %+v, want the new run's floor", cov) + } + h.noteScan("r", later.Add(time.Minute), time.Time{}, true) + if _, cov := h.snapshot([]string{"r"}, later); !cov.Full { + t.Fatalf("coverage = %+v, want full once a scan lists the whole history", cov) + } +} + +func TestAttributedPRHistoryPrunesPastRetention(t *testing.T) { + now := time.Now().UTC() + h := loadAttributedPRHistory(filepath.Join(t.TempDir(), AttributedPRHistoryFile), historyTestLogger()) + old := PullRequest{Repo: "r", Number: 1, HiveAttributed: true, State: "closed", CreatedAt: now.Add(-attributedPRHistoryRetention - 24*time.Hour), ClosedAt: now.Add(-attributedPRHistoryRetention)} + recent := PullRequest{Repo: "r", Number: 2, HiveAttributed: true, State: "closed", CreatedAt: now.Add(-48 * time.Hour), ClosedAt: now.Add(-24 * time.Hour)} + h.upsert([]PullRequest{recent}, nil, now) + // #1 was stored while young and has since aged past the retention. + h.file.Records["r#1"] = attributedPRRecordFrom(old) + // An open record not seen within the retention (its close was never + // observed) goes too; one seen recently stays. + h.file.Records["r#3"] = &attributedPRRecord{Repo: "r", Number: 3, State: "open", LastSeenAt: now.Add(-attributedPRHistoryRetention - time.Hour)} + h.file.Records["r#4"] = &attributedPRRecord{Repo: "r", Number: 4, State: "open", LastSeenAt: now.Add(-time.Hour)} + h.noteScan("r", now, time.Time{}, true) + if err := h.flush(now, nil); err != nil { + t.Fatal(err) + } + if h.file.Records["r#3"] != nil || h.file.Records["r#4"] == nil { + t.Fatalf("open records after prune: #3=%v #4=%v, want the stale one dropped", h.file.Records["r#3"], h.file.Records["r#4"]) + } + stored, cov := h.snapshot([]string{"r"}, now) + if got := historyNumbers(stored); len(got) != 1 || got[0] != 2 { + t.Fatalf("stored = %v, want only #2 kept", got) + } + if cov.Full || cov.Since.Before(now.Add(-attributedPRHistoryRetention-time.Minute)) { + t.Fatalf("coverage = %+v, want it to start at the retention cutoff", cov) + } +} + +// An error about the PR itself costs it a rework try, a GitHub hiccup one of +// its transient tries (and the next PR still gets its turn), and a +// budget/cancellation error nothing. Both caps are finite. +func TestAttributedPRBackfillReworkTriesCountOnlyPerPRErrors(t *testing.T) { + useTempReworkCache(t) + now := time.Now().UTC() + resetRESTAccountingForTest(t, now) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case strings.HasPrefix(r.URL.Path, "/repos/o/r/pulls/2/"): + http.Error(w, `{"message":"Not Found"}`, http.StatusNotFound) + case strings.HasPrefix(r.URL.Path, "/repos/o/r/pulls/3/"): + http.Error(w, `{"message":"unavailable"}`, http.StatusServiceUnavailable) + default: + writeJSON(t, w, []map[string]any{}) + } + })) + defer srv.Close() + c := NewClientForTest(srv.URL, "o", []string{"r"}, historyTestLogger()) + h := useHistoryAt(c, filepath.Join(t.TempDir(), AttributedPRHistoryFile)) + merged := func(n int, created time.Time) PullRequest { + return PullRequest{Repo: "r", Number: n, HiveAttributed: true, State: "closed", CreatedAt: created, ClosedAt: created.Add(time.Hour), MergedAt: created.Add(time.Hour)} + } + // Measured newest first: #3 (503), #2 (404), #1 (fine). + h.upsert([]PullRequest{merged(1, now.Add(-72*time.Hour)), merged(2, now.Add(-48*time.Hour)), merged(3, now.Add(-24*time.Hour))}, nil, now) + + _, stopped, _ := c.backfillAttributedPRRework(context.Background(), h, "r", 5, map[string]PRReworkStats{}) + if stopped { + t.Fatal("a 503 or 404 ended the whole pass") + } + if rec := h.file.Records["r#3"]; rec.ReworkTries != 0 || rec.ReworkTransientTries != 1 { + t.Fatalf("#3 (503) tries = %d transient = %d, want 0/1", rec.ReworkTries, rec.ReworkTransientTries) + } + if got := h.file.Records["r#2"].ReworkTries; got != 1 { + t.Fatalf("#2 (404) tries = %d, want 1", got) + } + if !h.file.Records["r#1"].ReworkKnown { + t.Fatal("#1 was not measured: the failing PRs ahead of it blocked it") + } + for i := 0; i < attributedPRBackfillReworkTransientTries; i++ { + c.backfillAttributedPRRework(context.Background(), h, "r", 5, map[string]PRReworkStats{}) + } + if pending := h.reworkPending("r"); len(pending) != 0 { + t.Fatalf("still pending after the caps: %v", historyNumbers(pending)) + } + if rec := h.file.Records["r#3"]; rec.ReworkTransientTries != attributedPRBackfillReworkTransientTries { + t.Fatalf("#3 transient tries = %d, want the cap %d", rec.ReworkTransientTries, attributedPRBackfillReworkTransientTries) + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + for name, err := range map[string]error{ + "shed": fmt.Errorf("listing reviews: %w", ErrPacingShed), + "cancelled": context.Canceled, + "429": &gh.ErrorResponse{Response: &http.Response{StatusCode: http.StatusTooManyRequests}}, + } { + if got := classifyAttributedPRBackfillErr(context.Background(), err); got != attributedPRBackfillErrStop { + t.Errorf("%s: class = %v, want stop", name, got) + } + } + notFound := &gh.ErrorResponse{Response: &http.Response{StatusCode: http.StatusNotFound}} + if got := classifyAttributedPRBackfillErr(ctx, notFound); got != attributedPRBackfillErrStop { + t.Errorf("error after the pass's context ended: class = %v, want stop", got) + } + if got := classifyAttributedPRBackfillErr(context.Background(), notFound); got != attributedPRBackfillErrItem { + t.Errorf("404: class = %v, want per-PR", got) + } +} + +// A closed PR older than what retention keeps is not stored again, so the +// next write does not have to prune it (and rewrite the file) every pass. +func TestAttributedPRHistoryUpsertSkipsPrunedPRs(t *testing.T) { + now := time.Now().UTC() + h := loadAttributedPRHistory(filepath.Join(t.TempDir(), AttributedPRHistoryFile), historyTestLogger()) + old := PullRequest{Repo: "r", Number: 1, HiveAttributed: true, State: "closed", CreatedAt: now.Add(-attributedPRHistoryRetention - time.Hour), ClosedAt: now.Add(-24 * time.Hour)} + h.upsert([]PullRequest{old}, nil, now) + if len(h.file.Records) != 0 || h.dirty { + t.Fatalf("records = %d dirty = %v, want the pre-retention PR skipped", len(h.file.Records), h.dirty) + } + h.file.PrunedThrough = now.Add(-10 * 24 * time.Hour) + capped := PullRequest{Repo: "r", Number: 2, HiveAttributed: true, State: "closed", CreatedAt: now.Add(-11 * 24 * time.Hour), ClosedAt: now.Add(-24 * time.Hour)} + h.upsert([]PullRequest{capped}, nil, now) + if len(h.file.Records) != 0 { + t.Fatalf("records = %d, want a PR created before pruned_through skipped", len(h.file.Records)) + } +} + +// A listing error that is not about the budget leaves that repo's cursor for +// the next pass and lets the pass go on to the next repo. +func TestAttributedPRBackfillListingErrorMovesToNextRepo(t *testing.T) { + useTempReworkCache(t) + now := time.Now().UTC() + resetRESTAccountingForTest(t, now) + var mu sync.Mutex + var listed []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("sort") == "created" { + mu.Lock() + listed = append(listed, r.URL.Path) + mu.Unlock() + } + switch r.URL.Path { + case "/repos/o/a/pulls": + http.Error(w, `{"message":"Not Found"}`, http.StatusNotFound) + case "/repos/o/b/pulls": + writeJSON(t, w, []map[string]any{backfillMergedPR(9, now.Add(-20*24*time.Hour))}) + default: + writeJSON(t, w, []map[string]any{}) + } + })) + defer srv.Close() + c := NewClientForTest(srv.URL, "o", []string{"a", "b"}, historyTestLogger()) + h := useHistoryAt(c, filepath.Join(t.TempDir(), AttributedPRHistoryFile)) + h.noteScan("a", now, now.Add(-14*24*time.Hour), false) + h.noteScan("b", now, now.Add(-14*24*time.Hour), false) + + c.syncAttributedPRHistory(context.Background(), []string{"a", "b"}) + mu.Lock() + got := strings.Join(listed, ",") + listed = nil + mu.Unlock() + if got != "/repos/o/a/pulls,/repos/o/b/pulls" { + t.Fatalf("listed %q, want repo a tried and repo b still listed", got) + } + if bf := h.file.Repos["a"].Backfill; bf.Listed || bf.NextPage != 1 { + t.Fatalf("repo a backfill = %+v, want its cursor kept for a retry", bf) + } + if bf := h.file.Repos["b"].Backfill; !bf.Listed || !bf.Done { + t.Fatalf("repo b backfill = %+v, want listed and done", bf) + } + c.syncAttributedPRHistory(context.Background(), []string{"a", "b"}) + mu.Lock() + got = strings.Join(listed, ",") + mu.Unlock() + if got != "/repos/o/a/pulls" { + t.Fatalf("second pass listed %q, want only repo a retried", got) + } +} + +// A repo that never scanned has nothing live or stored, so it must not make +// every window partial; with no scanned repo at all nothing is covered. +func TestAttributedPRHistoryCoverageIgnoresNeverScannedRepos(t *testing.T) { + now := time.Now().UTC() + day := 24 * time.Hour + h := loadAttributedPRHistory(filepath.Join(t.TempDir(), AttributedPRHistoryFile), historyTestLogger()) + h.noteScan("r", now.Add(-time.Hour), now.Add(-15*day), false) + h.file.Repos["r"].Backfill = attributedPRBackfillState{StartedAt: now, Horizon: now.Add(-30 * day), Listed: true, Done: true} + if _, cov := h.snapshot([]string{"r", "gone"}, now); cov.Full || !cov.Since.Equal(now.Add(-30*day)) { + t.Fatalf("coverage = %+v, want the scanned repo's horizon", cov) + } + if _, cov := h.snapshot([]string{"gone"}, now); !cov.Available || cov.Full || !cov.Since.Equal(now) { + t.Fatalf("coverage with no scanned repo = %+v, want nothing covered", cov) + } +} + +// Null entries in a hand-edited or truncated file are dropped on load +// instead of panicking inside enumeration. +func TestAttributedPRHistoryLoadDropsNullEntries(t *testing.T) { + path := filepath.Join(t.TempDir(), AttributedPRHistoryFile) + raw := `{"schema_version":1,"repos":{"r":null},"records":{"r#1":null,"r#2":{"repo":"r","number":2,"state":"closed","closed_at":"2026-11-01T00:00:00Z"}}}` + if err := os.WriteFile(path, []byte(raw), 0o600); err != nil { + t.Fatal(err) + } + h := loadAttributedPRHistory(path, historyTestLogger()) + if len(h.file.Repos) != 0 || len(h.file.Records) != 1 { + t.Fatalf("loaded repos=%d records=%d, want the null entries dropped", len(h.file.Repos), len(h.file.Records)) + } + now := time.Now() + h.snapshot([]string{"r"}, now) + h.reworkPending("r") + h.backfillFinish("r", now) + h.dirty = true + if err := h.flush(now, []string{"r"}); err != nil { + t.Fatal(err) + } +} + +// A repo whose live enumeration succeeded but whose closed-PR scan never ran +// is uncovered: its closed PRs are missing while its open ones are live. +func TestAttributedPRHistoryCoverageEnumeratedWithoutScanIsUncovered(t *testing.T) { + now := time.Now().UTC() + day := 24 * time.Hour + h := loadAttributedPRHistory(filepath.Join(t.TempDir(), AttributedPRHistoryFile), historyTestLogger()) + h.noteScan("r", now.Add(-time.Hour), now.Add(-15*day), false) + h.file.Repos["r"].Backfill = attributedPRBackfillState{StartedAt: now, Horizon: now.Add(-30 * day), Listed: true, Done: true} + h.noteEnumerated("new") + if _, cov := h.snapshot([]string{"r", "new", "gone"}, now); cov.Full || !cov.Since.Equal(now) { + t.Fatalf("coverage = %+v, want nothing covered while an enumerated repo has no scan", cov) + } +} + +// A repo whose last closed-PR scan is older than the live lookback is +// uncovered: PRs closed since are neither stored nor live. +func TestAttributedPRHistoryCoverageStaleScanIsUncovered(t *testing.T) { + now := time.Now().UTC() + day := 24 * time.Hour + h := loadAttributedPRHistory(filepath.Join(t.TempDir(), AttributedPRHistoryFile), historyTestLogger()) + h.noteScan("r", now.Add(-20*day), now.Add(-34*day), false) + h.file.Repos["r"].Backfill = attributedPRBackfillState{StartedAt: now.Add(-20 * day), Horizon: now.Add(-50 * day), Listed: true, Done: true} + if _, cov := h.snapshot([]string{"r"}, now); cov.Full || !cov.Since.Equal(now) { + t.Fatalf("coverage with a 20-day-old scan = %+v, want nothing covered", cov) + } + if _, cov := h.snapshot([]string{"r"}, now.Add(-19*day)); !cov.Since.Equal(now.Add(-50 * day)) { + t.Fatalf("coverage a day after that scan = %+v, want the backfill horizon", cov) + } +} + +// A walk whose listing keeps failing with a per-repo error gives up after +// the cap: no further calls, and the repo is not done, so coverage does not +// claim the horizon. +func TestAttributedPRBackfillListingFailuresCapStopsWalk(t *testing.T) { + useTempReworkCache(t) + now := time.Now().UTC() + day := 24 * time.Hour + resetRESTAccountingForTest(t, now) + var mu sync.Mutex + calls := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + calls++ + mu.Unlock() + http.Error(w, `{"message":"Not Found"}`, http.StatusNotFound) + })) + defer srv.Close() + c := NewClientForTest(srv.URL, "o", []string{"r"}, historyTestLogger()) + h := useHistoryAt(c, filepath.Join(t.TempDir(), AttributedPRHistoryFile)) + h.noteScan("r", now, now.Add(-14*day), false) + + for i := 0; i < attributedPRBackfillListFailures+3; i++ { + c.syncAttributedPRHistory(context.Background(), []string{"r"}) + } + mu.Lock() + got := calls + mu.Unlock() + if got != attributedPRBackfillListFailures { + t.Fatalf("listing calls = %d, want the cap %d", got, attributedPRBackfillListFailures) + } + if bf := h.file.Repos["r"].Backfill; bf.Done || bf.Listed { + t.Fatalf("backfill = %+v, want it given up, not done", bf) + } + if _, cov := h.snapshot([]string{"r"}, now); !cov.Since.Equal(now.Add(-14 * day)) { + t.Fatalf("coverage = %+v, want the live scan's floor, not the horizon", cov) + } +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 60e4bd32f1..3d4e5ed812 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -308,6 +308,10 @@ type Client struct { attributedClosedPRMu sync.Mutex attributedClosedPRCache map[string]attributedClosedPRCacheEntry attributedClosedPRErrors map[string]string + // attributedHistory persists attributed PRs past the bounded closed scan + // and runs the one-time backfill (attributed_pr_history.go); nil until + // SetAttributedPRHistoryPath. + attributedHistory atomic.Pointer[attributedPRHistory] // issueClaimCache remembers the claim read for an issue at a given // updated_at, so the per-issue comment fetch happens once per activity // change rather than once per enumeration. Guarded by issueClaimMu. @@ -2006,6 +2010,11 @@ func (c *Client) EnumerateActionable(ctx context.Context) (*ActionableResult, er return nil, fmt.Errorf("all %d repos failed to enumerate (last error: %w)", len(repos), lastFetchErr) } + // Write this pass's attributed PR history and take one bounded step of + // its one-time backfill. Best-effort: neither can fail enumeration, and + // a finished backfill makes no calls (attributed_pr_history.go). + c.syncAttributedPRHistory(ctx, repos) + RankActionableIssues(allIssues) // Review-priority order for the PR side of the snapshot: fixes > @@ -2643,7 +2652,10 @@ func (c *Client) fetchPRsWithDetails(ctx context.Context, repo string, clankerBu closed = c.cachedAttributedClosedPRs(repo) } attributed = append(attributed, closed...) - c.enrichAttributedPRRework(ctx, attributed) + reworkCache := c.enrichAttributedPRRework(ctx, attributed) + // Persisted past the live scan's lookback so the PRs-by-model windows + // keep a PR once the scan stops listing it (attributed_pr_history.go). + c.recordAttributedPRHistory(repo, attributed, reworkCache) if unclassified := totalPRs - breakdown.Total(); unclassified > 0 { breakdown.Other += unclassified @@ -2666,7 +2678,12 @@ func collectAttributedPRsFromList(repo string, prs []*gh.PullRequest) []PullRequ func (c *Client) fetchAttributedClosedPRs(ctx context.Context, owner, repoName, repo string) ([]PullRequest, error) { lookback := attributedClosedPRLookback() - cutoff := time.Now().Add(-lookback) + scanAt := time.Now() + cutoff := scanAt.Add(-lookback) + // oldest is the oldest updated time listed, the scan's floor when the + // page cap stops it before the cutoff; complete means the list ran out. + oldest := scanAt + pastWindow, complete := false, false opts := &gh.PullRequestListOptions{ State: "closed", Sort: "updated", @@ -2679,7 +2696,6 @@ func (c *Client) fetchAttributedClosedPRs(ctx context.Context, owner, repoName, if err != nil { return nil, fmt.Errorf("listing closed PRs for %s/%s: %w", owner, repoName, err) } - pastWindow := false for _, pr := range prs { if pr == nil { continue @@ -2688,16 +2704,28 @@ func (c *Client) fetchAttributedClosedPRs(ctx context.Context, owner, repoName, pastWindow = true break } + if updated := pr.GetUpdatedAt().Time; !updated.IsZero() && updated.Before(oldest) { + oldest = updated + } if meta, ok := ParseAttributionTrailer(pr.GetBody()); ok { out = append(out, pullRequestAttributionRecord(repo, pr, meta)) } } - if pastWindow || resp == nil || resp.NextPage == 0 { + if pastWindow { + break + } + if resp == nil || resp.NextPage == 0 { + complete = true break } opts.Page = resp.NextPage } c.storeAttributedClosedPRs(repo, out) + floor := oldest + if pastWindow { + floor = cutoff + } + c.attributedPRHistory().noteScan(repo, scanAt, floor, complete) return out, nil } @@ -2708,9 +2736,15 @@ func AttributedClosedPRLookback() time.Duration { } func attributedClosedPRLookback() time.Duration { - raw := strings.TrimSpace(os.Getenv(attributedClosedPRLookbackEnv)) + return lookbackFromEnv(attributedClosedPRLookbackEnv, attributedClosedPRDefaultLookback) +} + +// lookbackFromEnv reads a positive duration from env: a Go duration ("336h") +// or a day count ("14", "14d"). Unset or invalid is def. +func lookbackFromEnv(env string, def time.Duration) time.Duration { + raw := strings.TrimSpace(os.Getenv(env)) if raw == "" { - return attributedClosedPRDefaultLookback + return def } if d, err := time.ParseDuration(raw); err == nil && d > 0 { return d @@ -2723,7 +2757,7 @@ func attributedClosedPRLookback() time.Duration { if days, err := strconv.Atoi(raw); err == nil && days > 0 { return time.Duration(days) * 24 * time.Hour } - return attributedClosedPRDefaultLookback + return def } func (c *Client) storeAttributedClosedPRs(repo string, prs []PullRequest) { diff --git a/src/pkg/github/pr_rework.go b/src/pkg/github/pr_rework.go index 5353416b3d..41e5fe6360 100644 --- a/src/pkg/github/pr_rework.go +++ b/src/pkg/github/pr_rework.go @@ -47,9 +47,12 @@ type prCommentEvent struct { CreatedAt time.Time } -func (c *Client) enrichAttributedPRRework(ctx context.Context, prs []PullRequest) { +// enrichAttributedPRRework stamps each attributed PR's rework stats, from the +// rework cache or a fetch, and returns the cache as updated so the caller can +// reuse it without reading the file again. +func (c *Client) enrichAttributedPRRework(ctx context.Context, prs []PullRequest) map[string]PRReworkStats { if c == nil { - return + return nil } cache := loadPRReworkCache() changed := false @@ -75,6 +78,7 @@ func (c *Client) enrichAttributedPRRework(ctx context.Context, prs []PullRequest if changed { storePRReworkCache(cache) } + return cache } // MergedPRReworkCommits returns the number of commits pushed after the first