Skip to content
Merged
5 changes: 5 additions & 0 deletions docs/incidents/xw_every_open_pr_has_one_state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# xw. `xw_every_open_pr_has_one_state`

**What happened:** A team asked which pull requests were open in its repositories and what each one needed. Nothing could say. The gateway dropped every event of a pull request CoderHelm hadn't opened, the reviewer's records covered only the pull requests it reviewed and expired after 30 days, and the run page, the reviewer's auto-merge gate and the reviewer page each read what a pull request needed in their own way, so they could disagree.

**The guarantee:** Every open pull request in the team's connected repositories has one record (`pr_record`), written from its events whoever opened it (pull request, review, comment, check run, check suite, status and push) and from a bounded sync at install and once an hour, with no expiry while it is open. Each one has exactly one state from one function (`pr_needs::decide`); one whose facts aren't known yet is `unknown`, shown and counted, never dropped. The run's watch stores the standing that function decides and the reviewer's auto-merge gate merges only what it says is ready, so the gates and the pull requests list can't disagree. The list is read under the session's team only, and a record of another team never reaches a row.
5 changes: 5 additions & 0 deletions docs/incidents/xz_org_overview_facts_only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# xz. `xz_org_overview_facts_only`

**What happened:** A leader opened the pull requests page to understand the state of the org's pull requests and found only a list: nothing said what was stuck, on whom, or whether that was usual for the org. The overview above the list answers that, and it is the one place a page could rank people, print a number nothing computed, or mix two teams' pull requests.

**The guarantee:** `GET /api/prs/overview` (`common::pr_overview::build`) covers every pull request in the team's repositories, people's, CoderHelm's and other bots', and returns computed facts only. No per-person ranking, count or duration is ever returned; a GitHub login appears only as the owner of one item under "Needs attention". Every number in the summary is the value of a fact in `facts`, and the words between them carry no numbers of their own. Thresholds are percentiles of the scope's own history (a repo's own from 30 merges in 90 days, else the org's), no rate is shown on fewer than 10 events, and what CoderHelm doesn't store yet is `null` and named in `missing`, never estimated. The Stale tile and the Stale panel's headline are one number. An org with no CoderHelm pull requests gets the whole overview. Every read is keyed by the session's team, and the short cache by the installation, the team and the scope, listed in `bm_teams_never_cross`.
46 changes: 46 additions & 0 deletions docs/pull-requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# The pull requests view
Comment thread
coderhelm[bot] marked this conversation as resolved.
Resolved

Every open pull request in a team's connected repositories, people's as much as CoderHelm's: where it is, who worked on it, the one state it is in, and what it needs next from whom. The dashboard's **Pull requests** page reads it from `GET /api/prs`.

## The record

One settings-table item per pull request (`pk` = team id, `sk = PRREC#<owner/repo>#<number>`), its body a JSON document written with a version check (`services/common/src/pr_record.rs`).

- **Written from the events CoderHelm already receives**, for every pull request: `pull_request`, `pull_request_review`, review and issue comments, `check_run`, `check_suite`, `status` and `push`. A `status` or `push` event finds its pull request through a pointer item by head commit (`PRRECHEAD#`) or branch (`PRRECREF#`).
- **Kept honest by a sync** (`services/worker/src/passes/pr_records.rs`): one GraphQL search per organization of the team's connected repositories. It fills what events don't carry (the review decision, the branch's rules, conflicts, open conversations), adds pull requests opened before the install, and removes records of pull requests no longer open (only when the search was read to its end, and never one an event touched in the last hour).
- **Events and the sync never undo each other.** The sync never reopens a record an event closed; each field both write (state, title, draft, head, base, labels, review requests, reviews, review decision, checks, mergeability, auto-merge, the merge queue, size) keeps the time an event last wrote it, and the sync writes that field only when its search was read after that time. A queue, auto-merge or review change that lands while a page is in flight survives the page. The reviews and review requests the sync reads replace the record's lists, so a dismissed review leaves it. A list cut short by its page size is marked partial: a required check that may be on a later page reads as unknown, never as running. The review decision is also worked out at each review and review request event, and bypass and coverage use it only when it is newer than the last review.
- **No expiry while open.** A closed or merged pull request is kept for three days.
- **What it holds:** the author; who worked on it (committers, reviewers, commenters, and who asked CoderHelm); requested reviewers and teams; each person's latest review; the review decision; the checks on the head against the branch's required checks; conflicts; draft; open conversations; labels; links to stacked pull requests, to the same run's other pull requests (`plan_repos`, `repo_ci`) and to the ones named under the description's Compatibility heading; CoderHelm's run; a timeline of the newest events; and the wait ledger (who held the turn, and since when).

## History, the change log and the nightly snapshot

What the org overview reads, kept so the page never has to be migrated (`services/common/src/pr_history.rs`).

- **Every duration starts at ready for review**: when the pull request left draft, or its creation when it never was one. Human review leaves out the author, CoderHelm and every other bot.
- **The change log.** Each record keeps its turn changes (who held the turn, the state, the turn and the stage, and since when), every review submitted (people's and CoderHelm's, marked human or not), every review request with when it was taken back, and the head's checks turning red, pending or green. Time in a stage and time waiting on a person are read from it.
- **History.** When a pull request merges or closes, its history row (`sk = PRHIST#<owner/repo>#<number>`) is written and kept 400 days: ready, first human review, CoderHelm's first review, final approval, merge or close and who merged, rounds of changes requested, size, author type, CoderHelm's part (author, fixer, reviewer or none) with its runs and review sessions, the change log, and links.
- **Reverts and follow-ups.** A merged revert ("Reverts owner/repo#n") marks the pull request it reverts; a merged pull request that names an earlier merged one within 14 days of its merge marks it as followed up. Both are labeled a proxy for a failed change.
- **Every pull request, whoever wrote it.** Nothing filters to CoderHelm's pull requests: the webhooks write people's, bots' and CoderHelm's alike, and the sync keeps every author in the team's connected repositories.
- **The history backfill.** At install and on every hourly reconcile, each organization's closed and merged pull requests of the last 14 months are read into history, a month at a time from the newest, at most 5 search pages per organization per sweep. A held budget or GitHub's rate limit pauses it with the reason and the time; the next sweep resumes from the stored page cursor. Once all 14 months are read, each sweep reads the pull requests closed since its last day. The list shows the progress per organization, with the history rows read per repository.
- **The nightly snapshot** (`sk = PRSNAP#<scope>#<day>`, kept 400 days), once a day per team and per org and repository (the first 300): open pull requests by stage and turn, how many are past the scope's p85, arrivals, merges (by author type), closed without merging, reviews asked and done, reverts, and the percentiles: cycle time p50, p85 and p95 over 90 days, first human review p50 and p85 over 8 weeks, and each stage's p85. A repository with fewer than 30 merges in 90 days uses its org's percentiles and says so; an org with less than 8 weeks of history is marked young, so the page shows external bands instead. The job reads only the team's own records and history (at most 25,000 history rows) and makes no GitHub requests.

## One state per pull request

`services/common/src/pr_needs.rs` decides the state from the facts, first match wins: merged, closed, in the merge queue, left the queue, CoderHelm working, needs a decision, draft, waiting on another pull request, conflicts, checks failing, changes requested, re-review, needs a reviewer, needs review, needs a code owner, open conversations, checks running, ready with auto-merge on, ready to merge. A pull request whose facts aren't known yet is **not synced yet** (`unknown`): shown and counted, never dropped.

Each state has a turn (a person, CoderHelm, the machines, or done) and one sentence that names the block, who acts, and the rule's source when a rule is the reason (the repository's rule, CODEOWNERS, a required check, the team's own rule). "Ready" is said only when every requirement is met.

The same function is the merge gates' decision: the run's pull request watch stores the standing it decides (`run_standing`), and the reviewer's auto-merge gate merges only what it says is ready. The list, the run page and the gate can't disagree.

## The API

- `GET /api/prs`: filters `org`, `repo`, `turn` (`you`, `person`, `coderhelm`, `machines`, `done`), `state`, `person`, `q`, `view` (`my_turn`, `coderhelm_working`, `needs_decision`, `ready`, `stale`); `limit` (up to 200) and `cursor`, with `next` on the response. Counts by turn, state, repository, organization and view come over the scope before the other filters.
- `GET /api/prs/detail?repo=&number=`: the row, the path to merge, the wait ledger and the timeline with CoderHelm's runs.

Both need the "view" capability. The team is always the session's; a record of another team never reaches a row (`docs/incidents/xw_every_open_pr_has_one_state.md`).

## Cost

- Reading the list costs no GitHub requests: it reads the team's records, its runs on them (one batched read) and the reviewer's records, and reuses them for 10 seconds per installation and team.
- The sync spends from the installation's shared GitHub budget as `sweep:dashboard_prs`: at most 60 requests an hour: at most 10 pages of 30 open pull requests and 5 pages of closed ones per organization each time, once an hour per team and once at install. It runs only while the budget leaves the runs their share.
- Each webhook costs one read and one write of its record.
Loading