diff --git a/README.md b/README.md index 1028a651..bfc8a7b9 100644 --- a/README.md +++ b/README.md @@ -510,6 +510,7 @@ comes last, and a missing value prints as `-`: | `checkpoint` | Each bot reviewer that approves or requests changes, such as one whose approval the base requires, its round, and its review's URL, when that review is of the head and its latest verdict there, ignoring plain comments, is neither an approval nor a dismissal, at the end of its third, sixth, ... round: [`/quest-review`'s design checkpoint](#review-locally) for an online reviewer. A round is one commit it reviewed; a dismissed review counts as one that approves or requests changes. Only printed when due. | | `reacted:` | Actor, time, and the PR or review reacted to, as `quest wait` names reactions. | | `thread` | Node ID, `current` or `outdated`, last commenter, latest comment URL, and `path:line` of each unresolved thread. | +| `signal` | `finding` or `decision`, its URL, who posted it, and its text, for each [signal](#signals-that-hold-a-head) holding the head. | | `hold` | Label, acquisition token, acquisition time, verified GitHub event API URL, and age; `hold -` when no label holds it. Unknown acquisition facts print as `-` and `unknown age`. | | `starter-hold` | A draft its starter owns: last activity time, PR URL, and `last active` age. Last activity is not when ownership began. | @@ -533,7 +534,7 @@ requirement names one, its app, as GitHub and `quest land` do, and leaves out same facts as one object with the fields `url`, `state`, `draft`, `head`, `mergeable`, `mergeState`, `reviewDecision`, `autoMerge`, `mergeQueue`, `landingStopped`, `checks` (`state`, `passed`, `failing`, `pending`), `reviews`, `checkpoints` (`reviewer`, `round`, `url`), `reactions`, -`threads`, `holds` (`label`, `token`, `takenAt`, `age`, `eventUrl`), and `starterHold` (`lastActive`, `age`, `url`, or null). Ownership uses the state commands' shared reader without changing labels, and event links match the opaque acquisition token against GitHub's event listing. Long lists are paged; reactions are the newest 100 per PR or review, +`threads`, `signals` (`kind`, `url`, `author`, `text`), `holds` (`label`, `token`, `takenAt`, `age`, `eventUrl`), and `starterHold` (`lastActive`, `age`, `url`, or null). Ownership uses the state commands' shared reader without changing labels, and event links match the opaque acquisition token against GitHub's event listing. Long lists are paged; reactions are the newest 100 per PR or review, as in `quest wait`. Any PR state exits 0, invalid arguments exit 2, and other failures exit 1. @@ -584,7 +585,10 @@ quests skips it. While the base requires an approval the PR lacks, such as an online reviewer's that a ruleset requires (GitHub's review decision is `review_required` or `changes_requested`), it only arms auto-merge, which waits for the approval, and refuses where nothing would wait, since an admin's token -would merge or enqueue past the requirement. It enables auto-merge bound to that SHA, or merges at once when nothing is +would merge or enqueue past the requirement. +It also refuses while a [signal](#signals-that-hold-a-head) holds the head, +naming each. +It enables auto-merge bound to that SHA, or merges at once when nothing is left to wait for. `--squash` or `--rebase` picks the method, and it refuses one the repository disallows; otherwise it uses the only method the repository allows, or else a merge commit, and refuses when merge commits are off and more @@ -889,6 +893,35 @@ backoff. Grant the App the base ruleset's bypass and the read permissions needed to inspect branch protection. The bypass actor's API merge of a pinned PR with the unreported required `quest land` check must be verified on the live host. +### Signals that hold a head + +A signal holds a PR's head from landing until it is fixed or answered, since +the review its pick relied on predates it. `quest merge --land` refuses while +one holds, `quest status` lists each as a `signal` line, and a queue takes a +PR that draws one out, ejecting it as `unanswered_signal`, even while it waits +to merge. Nothing waits for a reviewer: a signal the queue reads before the +merge ends the pick. There are two kinds: + +- `finding`: an online reviewer's (a bot's, or a review whose body opens + `Automated review by `) on the head. A review thread it opened holds + until it is resolved or a writer other than the reviewer replies. A `[P]` + line in its latest review over the head, counting one that only opens + threads, holds until a writer's later comment links that review. +- `decision`: a writer's comment that opens with `Maintainer decision`, not in + a quote or code block, posted after the head's commit, which therefore cannot apply it. A + newer commit answers it, as does a writer's later comment that links it. + +The land fixer's commit reads neither kind, so it answers none: a head the +fixer put back in the queue still counts the findings on every head its pick's +rounds replaced, and the decisions posted since that pick, wherever the queue +judges it (the fixer's re-enqueue, `quest land watch`'s dequeue, and `quest land +serve`'s queue), however the head got queued. The carry is its own record, written +before the fixer publishes the head and kept through handbacks, holds, removals, +and later picks, so only a writer's push, which moves the head away, ends it. + +A writer can push to the repository: an owner, or a member or collaborator +whose permission GitHub reports as write or higher. A bot is never one. + ## Watch the merge queue with quest land watch `quest land watch` runs on a maintainer's host, one process per base branch of a repository, @@ -900,7 +933,10 @@ which offers no merge queue. Each pass reads the base's rules GitHub's queue on a repository switches its watcher with no host change; a pass that cannot read them does nothing. It reads them with a `contents: read` token, so an installation granted only one mode's permissions runs it. On GitHub's queue it tells each PR the queue -drops unmerged why. With `--fixer`, it also runs the land fixer on each PR the +drops unmerged why, and takes a queued or armed PR whose head draws a +[signal](#signals-that-hold-a-head) out of the queue, disarming its auto-merge +and saying why, even one another session holds, since GitHub's queue ignores +hold labels. With `--fixer`, it also runs the land fixer on each PR the queue dropped for a failure or a conflict, and puts it back at the back of the queue. It polls; the host needs no public endpoint. Its base is resolved once at startup, from `--base` or the default branch, so a renamed default branch needs a @@ -1034,10 +1070,11 @@ pull request GitHub refuses to enqueue says so and waits for a maintainer. It shares `quest land serve`'s App key, state directory, private repository, and locks, so the two never run for the same repository at once. Without a -fixer its token asks for `contents: read`, `pull_requests: write`, -`actions: read`, which the App needs for the logs, and `merge_queues: write`, -which `enqueuePullRequest` needs. With one it also asks for `contents: write`, -for arming auto-merge and pushing fixes, and `checks: read` and `statuses: read`, +fixer its token asks for `contents: write`, which disarming a signaled pull +request's auto-merge needs, `pull_requests: write`, `actions: read`, which +the App needs for the logs, and `merge_queues: write`, which +`enqueuePullRequest` needs. With one, which also arms auto-merge and pushes +fixes, it also asks for `checks: read` and `statuses: read`, for the PR's own checks the fixer reads; a push that carries a workflow change asks for `workflows: write`. diff --git a/assets/AGENTS.md b/assets/AGENTS.md index 199341d6..50666ad9 100644 --- a/assets/AGENTS.md +++ b/assets/AGENTS.md @@ -195,6 +195,7 @@ Start each sub-agent in the background with its own isolated worktree, since age Before starting or resuming one, run `quest agent admit` in the background and go on once it exits 0, handling reports meanwhile: it waits until the host has room for another agent. `quest agent start` and `resume` admit themselves. A sub-agent reports only when done or blocked, and never waits on GitHub CI, reviews, or merges; stay silent on its interim notifications. As each reports, explain its result in a few lines: its PR, whether it is still a draft, and its open decisions with recommendations. +Before dispatching a session to apply the user's answers to a PR's decisions, post them on the PR in a comment that opens with `Maintainer decision`: `quest merge` refuses the PR's current head until a newer commit, or a later comment linking the decision, answers it. Once two agents report the same blocker, such as a check failure that reproduces on the base or a reviewer outage, name it with its evidence in each later agent's brief, and report it once. A report that is an API error such as a rate limit or overload, or a cancellation the user did not ask for, is not a result. diff --git a/assets/skills/iterate.md b/assets/skills/iterate.md index 0227f1fc..7d6a2d0a 100644 --- a/assets/skills/iterate.md +++ b/assets/skills/iterate.md @@ -23,6 +23,7 @@ Record the settled decisions in this PR description's `## Decisions` section as Leave the open decisions of its follow-ups to the round that picks it to merge, and record answers your brief or the user gives as the guide's Creation section describes. Fix merge conflicts, failing CI checks, and the review findings already posted on the head. +Answer each `signal` line of `quest status ` that your push leaves: reply in the finding's thread, link its review in a comment, or link the decision in a comment saying how the head applies it. If `quest status ` shows auto-merge enabled, disable it with `gh pr merge --disable-auto` before any push: GitHub keeps it across a writer's push and would land the new head unreviewed. Once nothing is unfinished and no decision beyond its follow-ups' needs the user, finish with `/quest-review`, passing your token, whether the PR is a draft or already ready for review: it reviews the branch, pushes, and marks it ready. diff --git a/assets/skills/merge.md b/assets/skills/merge.md index f17896e7..29d0900b 100644 --- a/assets/skills/merge.md +++ b/assets/skills/merge.md @@ -15,7 +15,7 @@ The head is landable when: - the PR is not a draft; - a review counts for it, as below; -- every review finding posted on it, including automated reviewers' such as CodeRabbit or Codex, is fixed on the head or answered with a refutation, and no decision is open; +- every review finding posted on it, including automated reviewers' such as CodeRabbit or Codex, is fixed on the head or answered with a refutation, no decision is open, and `quest status` prints no `signal` line; - no check failed on it, required or not, its required checks passed, other than the gate's own `quest land` check, which never reports on a head, and it does not conflict with its base; - no review requests changes; an approval its base requires may still be to come, since the queue waits for it. diff --git a/assets/skills/run.md b/assets/skills/run.md index 573d4588..cab56e29 100644 --- a/assets/skills/run.md +++ b/assets/skills/run.md @@ -57,7 +57,8 @@ A `/quest-merge` pick covers the PR until it lands or needs the user: when `/que Order merges that would otherwise conflict. A PR that left the queue unmerged shows why under `landing-stopped` in `quest status `. Where the queue runs a fixer, such as `quest land`, it hands back only a decision: ask the user, with its handback comment's diagnosis and recommendation. -Without one, after `merge_conflict`, `failed_checks`, `changes_requested`, or `unresolved_conversations` the pick holds, even from an earlier session: dispatch `/quest-iterate`. After `checks_timed_out`, run `/quest-merge` again once; a second timeout of the same head is the user's call. +An `unanswered_signal`, which no fixer takes, holds the pick even with one: dispatch `/quest-iterate`. +Without a fixer, after `merge_conflict`, `failed_checks`, `changes_requested`, or `unresolved_conversations` the pick holds, even from an earlier session: dispatch `/quest-iterate`. After `checks_timed_out`, run `/quest-merge` again once; a second timeout of the same head is the user's call. Any other reason, such as a person removing it, is the user's call. ## Loop diff --git a/quest/m0/late-signals.md b/quest/m0/late-signals.md deleted file mode 100644 index 54e10a61..00000000 --- a/quest/m0/late-signals.md +++ /dev/null @@ -1,57 +0,0 @@ -# [M] A finding or decision posted on a head holds its landing - -## Goal - -A PR does not land past a review finding, or a recorded decision, posted -on its head after the review its pick relied on, including one posted -while it waits in a merge queue. `quest merge` refuses such a head, naming -the finding or comment, and a queued PR that draws one leaves the queue -until it is fixed or refuted. - -Non-goal: failed checks on the head, which `quest merge` already refuses -([moq-dev/quest#601](https://github.com/moq-dev/quest/pull/601)), and a -failed run on an earlier head, below. - -## Plan - -The 2026-11-11 [mistake audit](/quest/m0/mistake-audit.md) found 3 of the -week's 7 escapes landed past a signal already on the PR. The cases this -must refuse: - -- [moq-dev/moq#8375](https://github.com/moq-dev/moq/pull/8375): an online - review posted 65 seconds after enqueue described the bug - [moq-dev/moq#8504](https://github.com/moq-dev/moq/pull/8504) later - fixed, and the PR merged 15 minutes after it. -- [moq-dev/moq#8431](https://github.com/moq-dev/moq/pull/8431): a comment - recording the maintainer's decision said the head lacked a timeout, and - the same head was enqueued 10 minutes later, fixed by - [moq-dev/moq#8466](https://github.com/moq-dev/moq/pull/8466). - -Decided 2026-11-11: ✅ only comments that open with a fixed marker, such -as "Maintainer decision", hold a head, and the skills that record a -decision on a PR start writing it (rejected: any comment by a -write-access account after the head's commit; only review findings). -It would have caught moq-dev/moq#8431, whose comment opened that way, -without letting casual comments hold PRs. - -Agent's choices, for the implementer to revisit: - -- `quest merge` reads the reviews posted after the head's commit, and - the marked decision comments, and refuses while one holds a finding - or decision the head has not answered. - `quest status` prints them, so `/quest-merge` sees them before it - picks. -- The land watcher re-reads queued PRs and dequeues one that draws a - finding, so CONTRIBUTING's "online findings already posted are addressed - at merge, never waited for" still holds: nothing waits for a reviewer, - but a finding that arrives before the merge takes the PR out of the - queue until it is fixed or refuted. -- [moq-dev/moq#8372](https://github.com/moq-dev/moq/pull/8372) merged with - a red `platform.yml` dispatch run on an earlier head, which no check on - its final head showed. Leave it to moq-dev/moq, since one case is not a - class. - -## Related - -- [Mistake audit](/quest/m0/mistake-audit.md) - counts heads that landed - past a signal diff --git a/quest/m0/mistake-audit.md b/quest/m0/mistake-audit.md index 4e4f792c..c1147a9a 100644 --- a/quest/m0/mistake-audit.md +++ b/quest/m0/mistake-audit.md @@ -107,7 +107,7 @@ Check first next run: [moq-dev/quest#625](https://github.com/moq-dev/quest/pull/625) landed 2026-11-11T18:43Z: no data yet. - Heads that landed past a finding or decision, after - [late signals](/quest/m0/late-signals.md) lands. + [moq-dev/quest#652](https://github.com/moq-dev/quest/pull/652) lands. - Whether moq-dev/moq and moq-dev/moq.pro planned the runtime follow-ups of moq-dev/quest's 2026-11-11 run PR, and whether their escapes recurred. @@ -124,7 +124,7 @@ messages 2026-10-26 to 11-11, classed by one sub-agent, approximate): and 7, with 5 races. - Landed past an open signal: 3 in moq-dev/moq, 2 catchable and 1 maybe. A review finding posted after enqueue, a recorded decision the - head lacked, and a red platform run. [Late signals](/quest/m0/late-signals.md) takes the + head lacked, and a red platform run. [moq-dev/quest#652](https://github.com/moq-dev/quest/pull/652) takes the first two; `quest merge`'s refusal of failed checks ([moq-dev/quest#601](https://github.com/moq-dev/quest/pull/601)) covers none of them. diff --git a/src/cli/hold.ts b/src/cli/hold.ts index 0c3fa2de..58638c6e 100644 --- a/src/cli/hold.ts +++ b/src/cli/hold.ts @@ -21,6 +21,7 @@ import { ALLOWS, LAND_CHECK, METHOD_NAMES, contexts, enqueue } from "./land"; import { QUEUE_CHECK } from "./land-queue"; import type { LandingCheck } from "./landed"; import { classifyCheck } from "./status"; +import { describe, readSignals } from "./signals"; import { Refusal, ago } from "./start"; export type Method = "merge" | "squash" | "rebase"; @@ -260,6 +261,7 @@ export async function land(pr: PullRequest, request: LandRequest, deps: GitHubDe return { ...snapshot, unapproved: fresh.unapproved }; }; await passing(labels.github, pr, snapshot.url, sha); + await unsignaled(labels.github, pr, snapshot.url, sha); const findings = await request.check({ remote: `https://github.com/${pr.owner}/${pr.repo}.git`, base: snapshot.base, @@ -279,6 +281,8 @@ export async function land(pr: PullRequest, request: LandRequest, deps: GitHubDe const repository = object(await (await labels.github.request(repositoryPath(pr))).json()); const method = signalMethod(repository, needs.rules, named, snapshot.url, snapshot.base); await unchanged(); + // A finding or decision posted during the quest check above would otherwise ride into the queue. + await unsignaled(labels.github, pr, snapshot.url, sha); await enqueue(labels.github, snapshot.id, snapshot.head, method); return; } @@ -286,7 +290,9 @@ export async function land(pr: PullRequest, request: LandRequest, deps: GitHubDe if (snapshot.queue) { if (named !== undefined && named !== "merge") deps.warn(`${snapshot.url} lands through a merge queue, which applies its own merge method`); - await pick(labels, await unchanged(), named ?? "merge"); + const fresh = await unchanged(); + await unsignaled(labels.github, pr, snapshot.url, sha); + await pick(labels, fresh, named ?? "merge"); return; } const repository = object(await (await labels.github.request(repositoryPath(pr))).json()); @@ -318,6 +324,8 @@ export async function land(pr: PullRequest, request: LandRequest, deps: GitHubDe ); } const { unapproved } = await unchanged(); + // A decision posted since the check above leaves the head as it was, so `expectedHeadOid` would not refuse it. + await unsignaled(labels.github, pr, snapshot.url, sha); if (unapproved && repository.allow_auto_merge !== true) throw new Refusal( `${snapshot.url} lacks the approval its base requires, which a merge now would bypass; land it once it is approved`, @@ -351,6 +359,7 @@ export async function land(pr: PullRequest, request: LandRequest, deps: GitHubDe (await requirements(labels.github, pr, fresh.base)).checks.some((check) => check.name === LAND_CHECK) ) throw error; + await unsignaled(labels.github, pr, snapshot.url, sha); await submit("mergePullRequest"); } } @@ -372,6 +381,20 @@ async function passing(github: GitHub, pr: PullRequest, url: string, sha: string ); } +/** + * Refuse a head that an online reviewer's unanswered finding or a newer maintainer decision holds, naming each: + * the review a pick relied on predates them, so landing would skip them. + */ +async function unsignaled(github: GitHub, pr: PullRequest, url: string, sha: string): Promise { + const read = (await readSignals(github, pr, [pr.number])).get(pr.number)!; + if (read.head.toLowerCase() !== sha.toLowerCase()) + throw new Refusal(`${url} head moved to ${read.head}; check the new head before landing it`); + if (read.signals.length > 0) + throw new Refusal( + `${url} head ${sha} is held by ${describe(read.signals)}; push a fix, or answer each: reply in a finding's thread, or link its review or the decision in a comment that answers it; then land it again`, + ); +} + /** GraphQL error types that fault the token or the request, not the PR GitHub would enqueue. */ export const UNREFUSED = ["FORBIDDEN", "NOT_FOUND", "INSUFFICIENT_SCOPES", "INTERNAL"]; diff --git a/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index 51610d11..5e15aa22 100644 --- a/src/cli/land-fixer.ts +++ b/src/cli/land-fixer.ts @@ -17,7 +17,7 @@ import { import { constants as osConstants } from "node:os"; import { basename, join } from "node:path"; import { reviewSection } from "../core"; -import { GitHub, type GitHubDependencies, object, string } from "./github"; +import { GitHub, type GitHubDependencies, type ObjectValue, object, string } from "./github"; import { baseRemote, commandEnvironment, @@ -47,6 +47,7 @@ import { isPrepared, push, remoteHead } from "./land-head"; import { BASE_JOB, type Halt, haltNotice, readHalt, saveHalt } from "./land-halt"; import { isParked, parked } from "./land-park"; import { excerpt, type Failure } from "./land-serve"; +import { type Carried, describe, readSignals } from "./signals"; /** * Why a fixer's result cannot be published, which hands its PR back. A push GitHub refused, or a fix @@ -60,6 +61,8 @@ export class FixerRefusal extends Error { super(message); } } +/** A refusal because a finding or decision holds the head, which `quest status` reports as `unanswered_signal`. */ +export class SignalRefusal extends FixerRefusal {} class BaseCheckError extends Error {} /** A fix that edits protected paths beyond what the base merge carries: only a maintainer's push lands it. */ export class ProtectedEdit extends Error {} @@ -152,6 +155,8 @@ interface Job extends Process { base?: boolean; results?: FixResult[]; handbacks?: Record; + /** The PRs a finding or decision handed back, whose handback marker says `unanswered_signal`. */ + signaled?: number[]; /** Passes that failed to collect each PR, so one that keeps failing is set aside rather than holding up the rest. */ failures?: Record; /** PRs a handback is taking out of the queue, recorded before their auto-merge is disabled. */ @@ -175,7 +180,9 @@ export interface Round { outcome: Outcome; /** The head that failed. */ before: string; - /** The head the PR went back to the queue at, which a fix or merge moves; a handback has none. */ + /** The pin `before` stood on, when it was the gate's base merge on that pin rather than the pin itself. */ + pin?: string; + /** The head the round put back in the queue or published, which a fix or merge moves; a handback may keep it. */ after?: string; /** The failure's signature: what failed, so a recurrence after a fix can be told from a new failure. */ failure: string; @@ -206,9 +213,55 @@ export function history(state: string, number: number): History | undefined { const record = JSON.parse(readFileSync(path, "utf8")) as Omit & { rounds?: Round[] }; return { ...record, rounds: record.rounds ?? [] }; } +const carryPath = (state: string, number: number, head: string) => join(state, `carry-${number}-${head}.json`); +/** + * What the fixer's `head` of PR `number` carries from the pick it replaces: the heads its rounds replaced, with the + * pins the gate's base merges stood on, and when the pick was made. Its own record, written once when a round puts the + * head back, outlives handbacks, holds, and removals, since the person who picks the head again has answered nothing; + * a head no round put back, such as a writer's push, carries nothing. + */ +export function carriedBy(state: string, number: number, head: string): Carried | undefined { + const path = carryPath(state, number, head); + return existsSync(path) ? (JSON.parse(readFileSync(path, "utf8")) as Carried) : undefined; +} +/** Record what the head `round` put back carries, unless a record already holds it: a carry is never rewritten. */ +export function saveCarry( + state: string, + number: number, + pick: string, + round: Pick, +): void { + const after = round.after; + if (!after || !Number.isFinite(Date.parse(pick))) return; + const path = carryPath(state, number, after); + if (existsSync(path)) return; + // The heads it replaced, a gate merge's pin included, may carry an earlier pick's signals, which picking them + // again answered none of. An unchanged requeue of a gate merge still carries its pin's. + const replaced = round.pin ? [round.before, round.pin] : [round.before]; + const earlier = replaced.flatMap((head) => carriedBy(state, number, head) ?? []); + const heads = [...new Set([...earlier.flatMap((carry) => carry.heads), ...replaced])].filter( + (head) => head !== after, + ); + if (heads.length === 0) return; + const picked = [pick, ...earlier.map((carry) => carry.picked)].reduce((a, b) => + Date.parse(b) < Date.parse(a) ? b : a, + ); + save(path, { heads, picked } satisfies Carried); +} +/** What each of `pulls`' heads carries from a pick the land fixer replaced, by number. */ +export function carriedOf(state: string, pulls: ObjectValue[]): Map { + return new Map( + pulls.flatMap((pull) => { + const carried = carriedBy(state, Number(pull.number), string(pull.headRefOid)); + return carried ? [[Number(pull.number), carried] as const] : []; + }), + ); +} + /** Record `round` in `record`, replacing the same job's earlier one, and save it as PR `number`'s history. */ export function saveRound(state: string, number: number, record: History, round: Round): History { const next = { ...record, rounds: [...record.rounds.filter((item) => item.job !== round.job), round] }; + saveCarry(state, number, record.pick, round); save(historyPath(state, number), next); return next; } @@ -1081,9 +1134,19 @@ async function collectJob( { outcome, failure }, ); if (rule) error = stopReason(rule, { outcome, failure }); + // The gate's merge stood on the pin: either the plan's head is that merge, or the merge is newer than it. + const pinned = [entry.approved, entry.head].find((pin) => pin !== standing); + const pin = pinned ? { pin: pinned } : {}; round = rule - ? { job: name, outcome: "handback", before: standing, failure, rule } - : { job: name, outcome, before: standing, failure, ...(outcome === "handback" ? {} : { after: head }) }; + ? { job: name, outcome: "handback", before: standing, ...pin, failure, rule } + : { + job: name, + outcome, + before: standing, + ...pin, + failure, + ...(outcome === "handback" ? {} : { after: head }), + }; previous = saveRound(state, number, previous, round); } if (requeued) { @@ -1103,6 +1166,13 @@ async function collectJob( standing, deps.warn, ); + // Before the head moves, and for a fix of collateral too, which records no round. + const pinned = [entry.approved, entry.head].find((pin) => pin !== standing); + saveCarry(state, number, previous.pick, { + before: standing, + after: head, + ...(pinned ? { pin: pinned } : {}), + }); if (!job.published.includes(number)) { job.published.push(number); save(path, job); @@ -1116,6 +1186,22 @@ async function collectJob( attribution: author(result), }); + // The fix read no finding or decision on the pick it replaces, so those still hold it. + if (head !== entry.head) { + const [owner, repo] = job.plan.repository.split("/"); + const carried = carriedBy(state, number, head); + const held = ( + await readSignals( + github, + { owner: owner!, repo: repo! }, + [number], + carried && new Map([[number, carried]]), + ) + ).get(number)!; + if (held.head !== head) throw new FixerRefusal("head moved before it was re-enqueued"); + if (held.signals.length > 0) + throw new SignalRefusal(`its head is held by ${describe(held.signals)}`); + } // A parked PR requeued unchanged is still queued as it was; anything else is pinned anew. if (queuedAs !== entry.enabledAt || head !== entry.head) { // Save intent before enabling, so a lost response can be reconciled after restart. @@ -1131,6 +1217,7 @@ async function collectJob( } catch (failure) { if (!(failure instanceof FixerRefusal)) throw failure; if (failure.resolution) (job.resolutions ??= {})[number] = failure.resolution; + if (failure instanceof SignalRefusal) job.signaled = [...(job.signaled ?? []), number]; error = `${String(failure)}; run /quest-iterate before picking it again`; } } @@ -1148,8 +1235,7 @@ async function collectJob( const handback = error !== undefined || result?.action === "handback"; // A refused fix or requeue ends the round in a handback after all. if (handback && round && round.outcome !== "handback") { - const { after: _, ...rest } = round; - saveRound(state, number, history(state, number)!, { ...rest, outcome: "handback" }); + saveRound(state, number, history(state, number)!, { ...round, outcome: "handback" }); } if (error) { // Before leaving the queue, so a restart keeps this reason rather than finding it left. @@ -1187,8 +1273,13 @@ async function collectJob( "handback", author(result), handbackLog(job.log ?? "", !error && result?.action === "handback" ? result.log : undefined), - // The marker `quest status` reads, now that no ejection comment precedes this one. - job.parked && leaves ? (job.reason ?? "failed_checks") : undefined, + // The marker `quest status` reads, now that no ejection comment precedes this one; a signal that + // arrived while the fixer ran replaces the failure's reason, since that is why the pick stopped. + job.signaled?.includes(number) + ? "unanswered_signal" + : job.parked && leaves + ? (job.reason ?? "failed_checks") + : undefined, error && job.resolutions?.[number] ? (room) => resolutionSection(number, job.resolutions![number], room) : undefined, diff --git a/src/cli/land-head.ts b/src/cli/land-head.ts index a7d682d0..82db8cf8 100644 --- a/src/cli/land-head.ts +++ b/src/cli/land-head.ts @@ -1,6 +1,7 @@ // Trusted preparation and publication. Sandboxes and fixers never receive this repository or state. import { existsSync, readdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs"; import { join } from "node:path"; +import { carriedBy } from "./land-fixer"; import { gitAuthentication } from "./land-git"; import { GitHub, type GitHubDependencies, type Repository, object, string } from "./github"; import { @@ -48,7 +49,10 @@ export async function resolvePreparedQueue( target: Target, deps: GitHubDependencies, ): Promise { - const plan = await resolveQueue(repository, target, deps, (entry) => isPrepared(root, state, entry)); + const plan = await resolveQueue(repository, target, deps, { + acceptHead: (entry) => isPrepared(root, state, entry), + carried: (number, head) => carriedBy(state, number, head), + }); const numbers = new Set( readdirSync(state).flatMap((name) => { const match = /^head-(\d+)-.*\.json$/.exec(name); diff --git a/src/cli/land-watch-fix.ts b/src/cli/land-watch-fix.ts index baf66b21..29ab9508 100644 --- a/src/cli/land-watch-fix.ts +++ b/src/cli/land-watch-fix.ts @@ -6,6 +6,7 @@ import { isHoldLabel } from "../core"; import { array, GitHub, type GitHubDependencies, GitHubGraphQLError, object, type ObjectValue, string } from "./github"; import { type Queued, lacksApproval, queueHead } from "../github/merge"; import { UNREFUSED } from "./hold"; +import { type Carried, describe, readSignals } from "./signals"; import { baseRemote, enqueue, type Entry, type LandMethod, mergeBase, type RemoteRead } from "./land"; import { resolutionSection, type Resolution } from "./land-apply"; import { checksSection } from "./land-checks"; @@ -16,11 +17,13 @@ import { comment, conflictFailure, FixerRefusal, + SignalRefusal, handbackLog, fixerMarkdown, fixerText, history, type History, + carriedBy, historyPath, identity, importFix, @@ -98,6 +101,8 @@ interface WatchJob extends Process { requeued?: Queued; /** Why the round ended in a handback, recorded before posting it, so a restart keeps the reason. */ handback?: string; + /** A finding or decision on the head handed it back, so its marker says `unanswered_signal`. */ + signaled?: boolean; /** A validated fix whose push GitHub refused, handed back for `quest land apply`. */ resolution?: Resolution; /** Passes that failed to collect it, so one that keeps failing is set aside rather than holding a fixer slot. */ @@ -123,7 +128,7 @@ const JUDGED: Record = { }; const PULL = `query($id: ID!) { node(id: $id) { ... on PullRequest { - state baseRefName headRefOid mergeStateStatus reviewDecision mergeQueueEntry { enqueuedAt } autoMergeRequest { enabledAt } + number repository { nameWithOwner } state baseRefName headRefOid mergeStateStatus reviewDecision mergeQueueEntry { enqueuedAt } autoMergeRequest { enabledAt } labels(first: 100) { nodes { name } } headRepository { nameWithOwner } } } }`; @@ -695,13 +700,22 @@ async function collect( job.requeuing = head; save(path, job); } - job.requeued = await requeue(github, entry.id, head, job.base.ref, job.dropped, queue); + job.requeued = await requeue( + github, + entry.id, + head, + job.base.ref, + job.dropped, + queue, + carriedBy(state, number, head), + ); save(path, job); } catch (failure) { if (failure instanceof Held) return [...said, ...(await leave())]; if (!(failure instanceof FixerRefusal)) throw failure; handback = failure.message; if (failure.resolution) job.resolution = failure.resolution; + if (failure instanceof SignalRefusal) job.signaled = true; } } @@ -711,8 +725,10 @@ async function collect( // Every handback ends the pick with a round of its own, a crash's or a timeout's too, so the App's // next enqueue of a head this pick put back starts a new pick. if (round?.outcome !== "handback") { - const { after: _, ...rest } = round ?? { job: name, before: entry.head, failure }; - saveRound(state, number, history(state, number)!, { ...rest, outcome: "handback" }); + saveRound(state, number, history(state, number)!, { + ...(round ?? { job: name, before: entry.head, failure }), + outcome: "handback", + }); } if (handback) { job.handback = handback; @@ -731,7 +747,7 @@ async function collect( `handback ${name}`, attribution, handbackLog(job.log, !handback && result?.action === "handback" ? result.log : undefined), - job.reason, + job.signaled ? "unanswered_signal" : job.reason, job.resolution ? (room) => resolutionSection(number, job.resolution!, room) : undefined, ); finish(path, job); @@ -741,8 +757,9 @@ async function collect( /** * Put `head` back in `base`'s queue, at the back: GitHub's merge queue, or the watcher's own, by arming * auto-merge pinned to it. A PR queued or armed at it since the queue dropped it, at `dropped`, needs - * nothing more: a restart finds its own request, and a workflow may pick the fixer's push first. A - * refusal, or a PR retargeted from `base`, hands the PR back; a hold leaves it to the session that holds it. + * no enqueue: a restart finds its own request, and a workflow may pick the fixer's push first. A + * refusal, or a PR retargeted from `base`, hands the PR back; a hold leaves it to the session that holds it. A signal + * on `head` or on the `carried` pick it replaces, queued or not, takes it out of the queue and hands it back. */ async function requeue( github: GitHub, @@ -751,13 +768,37 @@ async function requeue( base: string, dropped: string, queue: QueueMode, + carried?: Carried, ): Promise { const how = (pull: ObjectValue): Queued => (pull.mergeQueueEntry != null ? "enqueued" : "armed"); let pull = await readPull(github, id); if (string(pull.baseRefName) !== base) throw new FixerRefusal(`retargeted from ${base} before it was re-enqueued`); if (string(pull.headRefOid) !== head) throw new FixerRefusal("head moved before it was re-enqueued"); + if (!queued(pull, dropped) && isHeld(pull)) throw new Held(); + // A finding or decision that arrived while the fixer ran holds the head as it would a writer's pick, even one a + // workflow already queued. The fixer's own commit, which read neither, answers none on the pick it replaces. + const [owner, repo] = string(object(pull.repository).nameWithOwner).split("/"); + const number = Number(pull.number); + const held = ( + await readSignals(github, { owner: owner!, repo: repo! }, [number], carried && new Map([[number, carried]])) + ).get(number)!; + if (held.head !== head) throw new FixerRefusal("head moved before it was re-enqueued"); + if (held.signals.length > 0) { + if (pull.mergeQueueEntry != null) { + await github.graphql("mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", { + id, + }); + // Dequeuing can disarm auto-merge too, and GitHub refuses to disable a request that is gone. + pull = await readPull(github, id); + } + if (string(pull.headRefOid) === head && queued(pull, dropped) && pull.autoMergeRequest != null) + await github.graphql( + "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", + { id }, + ); + throw new SignalRefusal(`its head is held by ${describe(held.signals)}`); + } if (!queued(pull, dropped)) { - if (isHeld(pull)) throw new Held(); try { if (queue.kind === "own") await enqueue(github, id, head, queue.method); else diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 98816a65..26ccbe41 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -18,7 +18,7 @@ import { lacksApproval } from "../github/merge"; import type { Entry, LandMethod, Plan } from "./land"; import { authenticatedRead } from "./land-git"; import { jobLog } from "./land-checks"; -import { fixerText, post } from "./land-fixer"; +import { COLLECTION_ATTEMPTS, carriedOf, fixerText, post } from "./land-fixer"; import { apiDependencies, daemonLoop, @@ -45,6 +45,7 @@ import { leaveOwnQueue, type OwnQueue, ownPass } from "./land-queue"; import { BUMP_PERMISSIONS, type BumpConnection, type BumpContext, bumpPass, checkTools, hostTools } from "./land-bump"; import { pruneLogs } from "./land-prune"; import { enabledHead, headBefore } from "./status"; +import { describe, readSignals } from "./signals"; export { readRemoval } from "./land-watch-fix"; @@ -139,6 +140,8 @@ interface Progress { handled: Record>; /** Per open PR, the armed pick GitHub refused to enqueue on the last pass, so each refusal is reported once. */ refused?: Record; + /** Per PR the watcher took out of the queue for a signal, the comment saying why that GitHub has not taken yet. */ + unsaid?: Record; } /** How far back of the last pass each pass reads again, for an event GitHub shows late. */ @@ -318,6 +321,160 @@ async function enqueueArmed( return output; } +/** + * Take each queued or armed PR whose head draws a signal, an online reviewer's unanswered finding or a newer + * maintainer decision, out of the merge queue and disarm its auto-merge, so neither it nor {@link enqueueArmed} lands + * it, and say why on it once per pick and head. Nothing waits for a reviewer: a signal that arrives before the merge + * ends the pick until a writer fixes or answers it and picks the PR again. A pull is read again just before, so a + * head pushed since is left for the next pass. + */ +async function dequeueSignaled( + github: GitHub, + repository: Repository, + base: string, + pulls: ObjectValue[], + progress: Progress, + saveProgress: () => void, + state: string, +): Promise { + const picked = pulls.filter((pull) => pull.mergeQueueEntry != null || pull.autoMergeRequest != null); + const output: string[] = []; + const read = await readSignals( + github, + repository, + picked.map((pull) => Number(pull.number)), + carriedOf(state, picked), + ); + for (const pull of picked) { + const number = Number(pull.number); + const head = string(pull.headRefOid); + const found = read.get(number); + if (!found || found.head !== head || found.signals.length === 0) continue; + const now = await readPull(github, string(pull.id)); + if (now.state !== "OPEN" || string(now.baseRefName) !== base || string(now.headRefOid) !== head) continue; + const pick = + now.autoMergeRequest != null + ? string(object(now.autoMergeRequest).enabledAt) + : now.mergeQueueEntry != null + ? string(object(now.mergeQueueEntry).enqueuedAt) + : null; + if (pick === null) continue; + // Out of the queue, no later pass reads the PR, so its comment is kept from before the mutations, which a lost + // response or a crash may apply unseen, until GitHub takes it or it no longer holds. + (progress.unsaid ??= {})[number] = { + id: string(pull.id), + pick, + head, + text: `quest land watch: ended this pull request's pick, ${[now.mergeQueueEntry != null ? "taking it out of the queue" : "", now.autoMergeRequest != null ? "disarming its auto-merge" : ""].filter(Boolean).join(" and ")}, since its head ${head} is held by ${describe(found.signals)}. Push a fix, or answer each by a thread reply or a comment linking its review or decision, then land it again.`, + failures: 0, + }; + saveProgress(); + // GitHub may disarm auto-merge with the dequeue, so the disarm reads the PR again first. Either one taken + // ends the pick and is said; when GitHub or a person moved first and neither was taken, nothing is said. + let taken = false; + try { + if (now.mergeQueueEntry != null) { + await github.graphql( + "mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", + { id: pull.id }, + ); + taken = true; + } + // Only the request this pass read is disarmed: a writer's push or new arming since is a newer pick. + const was = now.autoMergeRequest == null ? null : string(object(now.autoMergeRequest).enabledAt); + const after = taken ? await readPull(github, string(pull.id)) : now; + const armed = + string(after.headRefOid) === head && after.autoMergeRequest != null + ? string(object(after.autoMergeRequest).enabledAt) + : null; + if (armed !== null && armed === was) { + await github.graphql( + "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", + { id: pull.id }, + ); + taken = true; + } + } catch (error) { + if (!(error instanceof GitHubGraphQLError)) throw error; + const fresh = await readPull(github, string(pull.id)); + const ended = + fresh.state !== "OPEN" || + string(fresh.headRefOid) !== head || + (fresh.mergeQueueEntry == null && fresh.autoMergeRequest == null); + if (!ended) throw error; + // GitHub refused each mutation it was sent, so whoever ended the pick, it was not the watcher. + if (!taken) { + delete progress.unsaid[number]; + saveProgress(); + continue; + } + } + output.push( + `${now.mergeQueueEntry != null ? "dequeued" : "disarmed"} ${number} ${head} ${found.signals.length} signals`, + ); + } + output.push(...(await sayUnsaid(github, repository, progress, saveProgress))); + return output; +} + +/** + * Post each kept signal comment whose pick has ended with its PR open on the same head. One still in the queue on + * that pick waits for its dequeue; a closed, merged, pushed, or picked-again PR, or one GitHub refused the comment + * {@link COLLECTION_ATTEMPTS} times, drops it. + */ +async function sayUnsaid( + github: GitHub, + repository: Repository, + progress: Progress, + saveProgress: () => void, +): Promise { + const output: string[] = []; + for (const [key, entry] of Object.entries(progress.unsaid ?? {})) { + const number = Number(key); + const drop = () => { + delete progress.unsaid![number]; + saveProgress(); + }; + const now = await readPull(github, entry.id); + const pick = + now.autoMergeRequest != null + ? string(object(now.autoMergeRequest).enabledAt) + : now.mergeQueueEntry != null + ? string(object(now.mergeQueueEntry).enqueuedAt) + : null; + if (now.state !== "OPEN" || string(now.headRefOid) !== entry.head || (pick !== null && pick !== entry.pick)) { + drop(); + continue; + } + if (pick !== null) continue; + try { + await post( + github, + { repository: `${repository.owner}/${repository.repo}` }, + { number, enabledAt: entry.pick }, + entry.text, + `signaled ${entry.head}`, + "quest land watch", + "unanswered_signal", + ); + } catch (error) { + const reason = error instanceof Error ? error.message : String(error); + if (++entry.failures >= COLLECTION_ATTEMPTS) { + output.push( + `set aside the signal comment on ${number} after ${COLLECTION_ATTEMPTS} refusals: ${reason}`, + ); + drop(); + } else { + output.push(`unsaid ${number} ${entry.head}: ${reason}`); + saveProgress(); + } + continue; + } + drop(); + } + return output; +} + /** Every page of an Actions listing, whose rows are the response's `field`. */ export async function listed(github: GitHub, path: string, field: string): Promise { const rows: ObjectValue[] = []; @@ -695,6 +852,9 @@ export async function watchPass( save(path, progress); } } + output.push( + ...(await dequeueSignaled(github, repository, ref, pulls, progress, () => save(path, progress), options.state)), + ); output.push(...(await enqueueArmed(github, ref, pulls, progress, (text) => output.push(text)))); // The next pass reads from SETTLE before this one's start; nothing older can come back. const kept = new Date(Date.parse(started) - SETTLE).toISOString(); @@ -727,8 +887,8 @@ const same = (a: Repository, b: Repository) => /** * The installation token permissions the watcher asks for. On GitHub's merge queue, `api` reads the * queue's events, comments, and the `merge_group` logs (`actions: read`), and enqueues the armed picks - * GitHub left out of the queue, which `enqueuePullRequest` needs `merge_queues: write` for; with a fixer - * it also re-enqueues, which needs `contents: write` for arming auto-merge, edits the PR body's Review, + * GitHub left out of the queue, which `enqueuePullRequest` needs `merge_queues: write` for, and disarms a + * signaled PR's auto-merge, which needs `contents: write`; with a fixer it also re-enqueues, edits the PR body's Review, * and reads the PR's own checks for the fixer (`checks: read` and `statuses: read`). The `own` queue * also disarms and arms auto-merge, cancels a build's runs (`actions: write`), posts each pick's queue * check (`checks: write`), and reads the base's classic protection (`administration: read`), as @@ -759,7 +919,8 @@ export function watchPermissions( pushWorkflows: { ...push, workflows: "write" }, read: { contents: "read" }, }; - if (!fixing) return { api: { contents: "read", pull_requests: "write", actions: "read", merge_queues: "write" } }; + // Disarming a signaled PR's auto-merge needs contents: write, as arming it does. + if (!fixing) return { api: { contents: "write", pull_requests: "write", actions: "read", merge_queues: "write" } }; return { api: { contents: "write", diff --git a/src/cli/land.ts b/src/cli/land.ts index 73f63b18..92469098 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -20,6 +20,7 @@ import { import type { WorkflowRun } from "../github/checks"; import { appOf } from "../github/checks"; import { classifyCheck, reported, type Needs } from "./status"; +import { type Carried, describe, readSignals } from "./signals"; import type { Method } from "./hold"; import { committedSources, snapshot } from "./tree"; @@ -109,7 +110,8 @@ export type Reason = | "not_pinned" | "tree_mismatch" | "merge_refused" - | "fork_head"; + | "fork_head" + | "unanswered_signal"; /** The hidden line an ejection comment carries, which `quest status` reads. */ export const EJECTED = /^$/m; @@ -309,6 +311,27 @@ async function unresolved(github: GitHub, id: string): Promise { } } +/** + * What a landing PR's head carries unread: a head the gate merged the base into after its pin carries the pin's + * signals since the pick, and whatever the land fixer's history carries for the pin or the head. + */ +function carriedFrom(node: ObjectValue, carried: QueueOptions["carried"]): Carried | undefined { + const request = object(node.autoMergeRequest); + const head = string(node.headRefOid); + const approved = pinned(typeof request.commitBody === "string" ? request.commitBody : ""); + const fixed = carried?.(Number(node.number), approved ?? head); + if (approved === null || approved === head) return fixed; + return { heads: [approved, ...(fixed?.heads ?? [])], picked: fixed?.picked ?? string(request.enabledAt) }; +} + +/** How `resolveQueue` reads its picks beyond what GitHub says. */ +export interface QueueOptions { + /** Whether a head that moved since its pick is still the pick's, as the gate's own base merge is. */ + acceptHead?: (entry: Entry) => boolean; + /** What PR `number`'s `head` carries from a pick the land fixer replaced. */ + carried?: (number: number, head: string) => Carried | undefined; +} + /** * Read the queue: open PRs to `base` with auto-merge enabled, in the order it * was enabled. A PR is queued while its auto-merge commit body pins its current @@ -320,8 +343,9 @@ export async function resolveQueue( repository: Repository, target: Target, deps: GitHubDependencies, - acceptHead?: (entry: Entry) => boolean, + options: QueueOptions = {}, ): Promise { + const { acceptHead, carried } = options; const github = new GitHub(deps); const name = `${repository.owner}/${repository.repo}`; const settings = object(await (await github.request(repositoryPath(repository))).json()); @@ -408,6 +432,17 @@ export async function resolveQueue( const landing = nodes.filter((node) => node.autoMergeRequest !== null && node.isDraft !== true); const order = (node: ObjectValue) => string(object(node.autoMergeRequest).enabledAt); landing.sort((a, b) => order(a).localeCompare(order(b)) || Number(a.number) - Number(b.number)); + const held = await readSignals( + github, + repository, + landing.map((node) => Number(node.number)), + new Map( + landing.flatMap((node) => { + const carry = carriedFrom(node, carried); + return carry ? [[Number(node.number), carry] as const] : []; + }), + ), + ); for (const node of landing) { const request = object(node.autoMergeRequest); const head = string(node.headRefOid); @@ -441,6 +476,11 @@ export async function resolveQueue( eject("changes_requested", "a reviewer requested changes"); } else if (resolution && (await unresolved(github, entry.id)) > 0) { eject("unresolved_conversations", "it has unresolved review threads; resolve them and land it again"); + } else if (held.get(entry.number)?.head === head && held.get(entry.number)!.signals.length > 0) { + eject( + "unanswered_signal", + `its head is held by ${describe(held.get(entry.number)!.signals)}; push a fix, or answer each by a thread reply or a comment linking its review or decision, then land it again`, + ); } else { // The queue's first page holds the head's checks, unless the head moved from the pin since. const commit = object(object(array(object(node.commits).nodes)[0]).commit); diff --git a/src/cli/main.ts b/src/cli/main.ts index 743bbe23..96529d11 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -571,7 +571,8 @@ export function main(argv: string[], out: Output): number | Promise { "reviewer that approves or requests changes, its round, and its review, when that review is of the " + "head, while its latest verdict on the head is neither an approval nor a dismissal, and ends its third, sixth, ... round (one commit it reviewed), as " + "/quest-review's design checkpoint; a reacted: line per " + - "reaction on the PR or a review; and a thread line per unresolved review thread, led by its node ID. " + + "reaction on the PR or a review; a thread line per unresolved review thread, led by its node ID; and a " + + "signal line per finding or decision that holds the head from landing (see the README). " + "A missing value prints as -. --json prints the same facts as one deterministic object.\n\n" + "Uses GH_TOKEN, GITHUB_TOKEN, or gh auth token. Exits 0 for any PR state, 2 for invalid arguments, " + "and 1 for other failures.", @@ -645,7 +646,8 @@ export function main(argv: string[], out: Output): number | Promise { "lacks (GitHub's review decision is review_required or changes_requested), it only arms auto-merge, which waits " + "for it, and refuses where nothing would wait, since an admin's token merges or enqueues past the requirement. It refuses a head on which quest check " + "--no-stubs --base --branch finds anything, read from git objects fetched from the base repository, " + - "so nothing from the head runs, except findings the base already has; a base without quests skips it. It uses the method --squash or --rebase names, refusing one " + + "so nothing from the head runs, except findings the base already has; a base without quests skips it. It also refuses while an online reviewer's " + + "unanswered finding on the head, or a newer comment opening with 'Maintainer decision', holds it. It uses the method --squash or --rebase names, refusing one " + "the repository disallows; otherwise the only method the repository allows, or else a merge commit. " + "On a base with GitHub's merge queue, which applies its own method, it enqueues that head, or arms " + "auto-merge bound to it while required checks may be pending; a head behind the base is enqueued as " + @@ -780,7 +782,7 @@ export function main(argv: string[], out: Output): number | Promise { landCommand .command("watch") .description( - "Watch the base's queue as a daemon, one per base branch of a repository, resolved once at startup from --base or the default branch: GitHub's merge queue where the base's rules have a merge_queue rule, otherwise the watcher's own serial queue, decided again each pass from GET /rules/branches/, so enabling GitHub's queue switches the watcher with no host change; a pass that cannot read the rules does nothing. On GitHub's queue, once a minute, read the base's open PRs and the merge queue's removals since the last pass, and comment once on each PR the queue dropped unmerged, naming GitHub's reason and, for a failed `merge_group` build, the failing jobs and their logs' tails. A failed build or a conflict is recorded for the land fixer; a push since the removal, a person's removal, or an unknown reason ends the pick. A PR whose auto-merge was armed, by a writer or by the watcher, that is ready (its required checks passed and it does not conflict) but that GitHub never added to the queue is enqueued at the head auto-merge was armed on, never at a head pushed since. A pick is the latest enqueue by anyone, the App included (as for Dependabot's PRs), except the App's enqueues of heads the watcher itself put back in the queue for the pick. " + + "Watch the base's queue as a daemon, one per base branch of a repository, resolved once at startup from --base or the default branch: GitHub's merge queue where the base's rules have a merge_queue rule, otherwise the watcher's own serial queue, decided again each pass from GET /rules/branches/, so enabling GitHub's queue switches the watcher with no host change; a pass that cannot read the rules does nothing. On GitHub's queue, once a minute, read the base's open PRs and the merge queue's removals since the last pass, and comment once on each PR the queue dropped unmerged, naming GitHub's reason and, for a failed `merge_group` build, the failing jobs and their logs' tails. A failed build or a conflict is recorded for the land fixer; a push since the removal, a person's removal, or an unknown reason ends the pick. A PR whose auto-merge was armed, by a writer or by the watcher, that is ready (its required checks passed and it does not conflict) but that GitHub never added to the queue is enqueued at the head auto-merge was armed on, never at a head pushed since. A pick is the latest enqueue by anyone, the App included (as for Dependabot's PRs), except the App's enqueues of heads the watcher itself put back in the queue for the pick. A queued or armed PR whose head draws a signal, an online reviewer's unanswered finding or a newer maintainer decision, is taken out of the queue and its auto-merge disarmed, with a comment; the own queue ejects it, and the fixer hands it back rather than re-enqueue it, counting those on the head its fix replaced. " + `The own queue, for a base that requires the unreported \`${LAND_CHECK}\` check so GitHub never merges a pick itself, reads its picks as quest land serve does: PRs whose auto-merge \`quest merge --land\` armed pinned to their head, in arming order, with the same ejections. It tests one at a time: it merges the base into the pick with the quest merge driver, pushes that to quest-queue//, which the repository's queue workflow builds on push, and waits for the base's required checks other than \`${LAND_CHECK}\` on it, at most --timeout minutes. A pass lands a passing pick through quest land serve's landing path with --method, only while the base is still the one it tested; a moved base rebuilds it. A failed or timed-out build, or a conflict, disarms the PR's auto-merge and comments with the failing jobs, as GitHub's queue removes a PR, and is recorded for the land fixer; a re-enqueue arms auto-merge again, at the back. A writer's push, a person disarming auto-merge, or a hold ends the build. Each pick carries an informational \`quest queue\` check run with its position, the build in progress and its run, and the outcome. Queue branches go once their pick lands or leaves, and those an earlier process left are pruned on its first pass. ` + "With --fixer, a conflict is first merged with the base by the quest merge driver; otherwise the fixer runs in the background on the failure, at most --fixer-jobs at once, oldest removal first. A fixed or merged head is lease-pushed and recorded in Review, and each fixed, merged, or flaky head is re-enqueued at the back of the queue (or armed with auto-merge while its checks run). The pick keeps going while each round makes progress: a round with no new commit for a failure that isn't flaky, the failure a fix addressed recurring right after it, or three flaky rounds in a row hand it back, as does the fixer, with a comment. State keeps each removal handled, so a restart neither misses nor repeats one, each removal's full log, the fixer's jobs, each pick's rounds, and the own queue's build. It shares `quest land serve`'s App key, state directory, private repository, and locks on the same base, so the two never run beside each other there, while daemons on different bases run side by side, each with its own clone. On GitHub's queue the App needs `actions: read` for the logs, `merge_queues: write` to enqueue, and with a fixer `contents: write` to arm auto-merge and push, and `workflows: write` to push fixes that touch workflows. The own queue's App needs `contents: write`, `pull_requests: write`, `actions: write` to read logs and cancel builds, `checks: write`, `administration: read`, `workflows: write` for heads that touch workflows, and a bypass of the base's ruleset.", ) diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index 3f64eb12..1cc99f20 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -30,32 +30,36 @@ export function classify(body: string): Answer { } /** - * The finding lines of a review body, `[P1] ...` or `P1 — ...` with a dash after the bare marker, each keyed by its - * title and how many earlier lines of the body share that title. + * The finding lines of a review body, `[P1] ...` or `P1 — ...` with a dash after the bare marker, in a list item or + * bold or not, each with its title and a key of that title and how many earlier lines of the body share it. */ -export function bodyFindings(body: string): string[] { +export function bodyFindings(body: string): { title: string; key: string }[] { const seen = new Map(); - return [...body.matchAll(/^[ \t]*(?:[-*][ \t]+)?(?:\[P\d\]|P\d[ \t]+[—–-])[ \t:—–-]*(\S.*)$/gm)].map((match) => { + return [ + ...body.matchAll( + /^[ \t]*(?:(?:[-*]|\d+[.)])[ \t]+)?(?:\*\*|__)?(?:\[P\d\]|P\d[ \t]+[—–-])(?:\*\*|__)?[ \t:—–-]*(\S.*)$/gm, + ), + ].map((match) => { // A later round that relists a finding repeats its title, or its location when it leads; the rest may change. const title = match[1]! .split(/ [—–] | \(|: |\. /)[0]! - .replaceAll("`", "") + .replace(/[`*]|__/g, "") .replace(/\s+/g, " ") .trim() .toLowerCase(); const count = seen.get(title) ?? 0; seen.set(title, count + 1); - return count ? `${title} #${count + 1}` : title; + return { title, key: count ? `${title} #${count + 1}` : title }; }); } -const THREAD_COMMENT = "author { __typename login } body pullRequestReview { id }"; +const THREAD_COMMENT = "author { __typename login } authorAssociation body url pullRequestReview { id }"; const FIELDS = { - reviews: "id url author { __typename login } body submittedAt commit { oid }", + reviews: "id url state author { __typename login } authorAssociation body submittedAt commit { oid }", reviewThreads: "id isResolved resolvedBy { __typename login } " + `comments(first: 100) { pageInfo { hasNextPage endCursor } nodes { ${THREAD_COMMENT} } }`, - comments: "author { __typename login } body createdAt", + comments: "author { __typename login } authorAssociation body createdAt url", } as const; type Connection = keyof typeof FIELDS; const CONNECTIONS = Object.keys(FIELDS) as Connection[]; @@ -65,16 +69,23 @@ const page = (name: Connection, after: string) => /** PRs read per query; each brings at most 100 reviews, 50 threads of 100 comments, and 100 comments. */ const BATCH = 10; -/** A PR's last head, and its reviews, review threads, and comments as GitHub's GraphQL API gives them. */ -export type Activity = Record & { head: string }; +/** + * A PR's last head and its committer date, and its reviews, review threads, and comments as GitHub's GraphQL API gives + * them; `moved` when a later page found a newer head, so the pages may mix two heads' activity. + */ +export type Activity = Record & { head: string; committed: string; moved?: boolean }; /** Each PR's reviews, review threads, and comments, and its head, read in batches and then in pages. */ -async function read(client: GitHub, repository: Repository, numbers: number[]): Promise> { +export async function readActivity( + client: GitHub, + repository: Repository, + numbers: number[], +): Promise> { const pulls = new Map(); const more: { number: number; cursors: Partial> }[] = []; for (let start = 0; start < numbers.length; start += BATCH) { const batch = numbers.slice(start, start + BATCH); - const fields = `headRefOid ${CONNECTIONS.map((name) => page(name, "")).join(" ")}`; + const fields = `headRefOid commits(last: 1) { nodes { commit { oid committedDate } } } ${CONNECTIONS.map((name) => page(name, "")).join(" ")}`; const data = await client.graphql( `query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { ${batch .map((number) => `p${number}: pullRequest(number: ${number}) { ${fields} }`) @@ -87,7 +98,10 @@ async function read(client: GitHub, repository: Repository, numbers: number[]): if (value === null || value === undefined) throw new Error(`GitHub has no PR #${number} in ${repository.owner}/${repository.repo} for this token`); const pull = object(value); - const nodes = { head: String(pull.headRefOid) } as Activity; + const commit = object(object(array(object(pull.commits).nodes)[0] ?? {}).commit ?? {}); + if (commit.oid !== pull.headRefOid) + throw new Error(`the head of PR #${number} changed while it was read; retry`); + const nodes = { head: String(pull.headRefOid), committed: String(commit.committedDate) } as Activity; const cursors: Partial> = {}; for (const name of CONNECTIONS) { const connection = object(pull[name]); @@ -105,12 +119,14 @@ async function read(client: GitHub, repository: Repository, numbers: number[]): const names = CONNECTIONS.filter((name) => left[name] !== undefined); const data = await client.graphql( `query($owner: String!, $name: String!, $number: Int!, ${names.map((name) => `$${name}: String!`).join(", ")}) { ` + - `repository(owner: $owner, name: $name) { pullRequest(number: $number) { ${names + `repository(owner: $owner, name: $name) { pullRequest(number: $number) { headRefOid ${names .map((name) => page(name, `, after: $${name}`)) .join(" ")} } } }`, { owner: repository.owner, name: repository.repo, number, ...left }, ); const pull = object(object(data.repository).pullRequest); + // A later page may hold a review of a head pushed since the first read, which that head would misjudge. + if (pull.headRefOid !== nodes.head) nodes.moved = true; const next: Partial> = {}; for (const name of names) { const connection = object(pull[name]); @@ -129,9 +145,11 @@ async function read(client: GitHub, repository: Repository, numbers: number[]): for (let info = object(comments.pageInfo); info.hasNextPage === true;) { const data = await client.graphql( "query($id: ID!, $after: String!) { node(id: $id) { ... on PullRequestReviewThread { " + + "pullRequest { headRefOid } " + `comments(first: 100, after: $after) { pageInfo { hasNextPage endCursor } nodes { ${THREAD_COMMENT} } } } } }`, { id: String(thread.id), after: String(info.endCursor) }, ); + if (object(object(data.node).pullRequest ?? {}).headRefOid !== nodes.head) nodes.moved = true; const page = object(object(data.node).comments); all.push(...array(page.nodes)); info = object(page.pageInfo); @@ -142,7 +160,7 @@ async function read(client: GitHub, repository: Repository, numbers: number[]): } /** An account's login, a bot's as `name[bot]`, which GraphQL gives a bot author without the suffix. */ -function login(actor: unknown): string | null { +export function login(actor: unknown): string | null { if (actor === null || actor === undefined) return null; const { __typename, login } = object(actor); if (typeof login !== "string") return null; @@ -150,12 +168,15 @@ function login(actor: unknown): string | null { } /** Whose review this is: the name its body opens with, else its bot author, else nobody's. */ -function reviewerOf(review: ObjectValue): string | null { +export function reviewerOf(review: ObjectValue): string | null { const named = /^Automated review by (.+)/.exec(typeof review.body === "string" ? review.body : ""); if (named) return named[1]!.trim(); return object(review.author ?? {}).__typename === "Bot" ? login(review.author) : null; } +/** Whether an author is a bot account, such as a reviewer or the land watcher, whose comments answer nothing. */ +export const isBot = (actor: unknown) => actor != null && object(actor).__typename === "Bot"; + /** The anchor GitHub links a review by. */ const ANCHOR = /#pullrequestreview-\w+/g; @@ -227,7 +248,7 @@ export function onlineReviews(pull: Activity): OnlineReview[] { // A body finding is one title across a reviewer's passes; a pass after its last listing no longer finds it. const listed = new Map(); const lists = passes.map((pass, index) => { - const titles = bodyFindings(pass.body); + const titles = bodyFindings(pass.body).map((finding) => finding.key); for (const title of titles) { const id = JSON.stringify([pass.reviewer, title]); listed.set(id, { reviewer: pass.reviewer!, last: index, answer: listed.get(id)?.answer ?? null }); @@ -242,6 +263,7 @@ export function onlineReviews(pull: Activity): OnlineReview[] { )) { const author = login(comment.author); if (author !== null && reviewers.has(author)) continue; + if (isBot(comment.author)) continue; const body = String(comment.body ?? ""); const at = Date.parse(String(comment.createdAt)); const links = new Set([...body.matchAll(ANCHOR)].map((match) => match[0])); @@ -272,6 +294,6 @@ export async function readOnlineReviews( repository: Repository, numbers: number[], ): Promise> { - const pulls = await read(client, repository, numbers); + const pulls = await readActivity(client, repository, numbers); return new Map([...pulls].map(([number, pull]) => [number, onlineReviews(pull)])); } diff --git a/src/cli/signals.ts b/src/cli/signals.ts new file mode 100644 index 00000000..04c38a94 --- /dev/null +++ b/src/cli/signals.ts @@ -0,0 +1,211 @@ +// What holds a PR's head from landing besides its checks: an online reviewer's finding on the head that no writer has +// answered, and a maintainer decision recorded in a comment after the head was committed that nothing has answered. +import { type GitHub, type ObjectValue, type Repository, array, object, string } from "./github"; +import { type Activity, bodyFindings, isBot, login, readActivity, reviewerOf } from "./online-review"; + +/** One signal on a PR's head that holds it from landing until it is fixed or answered. */ +export interface Signal { + /** `finding`: an online reviewer's, on the head. `decision`: a marked comment newer than the head's commit. */ + kind: "finding" | "decision"; + /** The review, thread, or comment that raised it. */ + url: string; + author: string; + /** The finding's title or thread's first line, or the decision's first line. */ + text: string; +} + +/** The picked heads whose signals a later commit carries unread, and when the pick was made. */ +export interface Carried { + heads: string[]; + picked: string; +} + +/** + * The marker a comment's first line opens with to record a maintainer decision, after markup such as `**` but not in + * a quote or code, which four leading spaces or a tab also start. + */ +export const DECISION = /^ {0,3}(?![ \t])[^A-Za-z>`~\n]*Maintainer decision\b/i; + +/** Repository permissions that can push, whose decisions and answers count. */ +const WRITE = ["admin", "maintain", "write"]; + +/** A body's first line of prose, past markup such as a bot's HTML comments and images, cut short. */ +const firstLine = (body: string) => + body + .replace(/|<[^>\n]*>/g, "") + .split("\n") + .map((line) => line.replace(/\*\*|__/g, "").trim()) + .find((line) => /[A-Za-z]/.test(line)) + ?.slice(0, 120) ?? ""; + +/** The `#...` anchor of a GitHub URL. */ +const anchorOf = (url: string) => url.slice(url.lastIndexOf("#")); + +/** Whether `body` links `url`'s anchor whole, not as the start of a longer ID such as `#issuecomment-90` for `-9`. */ +const links = (body: string, url: string) => + body + .split(anchorOf(url)) + .slice(1) + .some((rest) => !/^[\w-]/.test(rest)); + +/** + * The signals holding `pull`'s head, given which accounts can push to its repository. A finding is an online + * reviewer's, from its latest pass over the head: a review thread it opened that is unresolved with no writer's reply, + * or a `[P]` line of that pass's body that no writer's later comment links. A + * decision is a writer's comment opening with {@link DECISION}, posted after the head was committed, that no writer's + * later comment links: a head committed before a decision cannot have applied it. A bot is never a writer. + * `carried` names heads a writer picked that a commit since carries forward unread, such as the land fixer's: findings on + * them hold too, and so does a decision posted after its pick. + */ +export function signals(pull: Activity, writers: ReadonlySet, carried?: Carried): Signal[] { + const writer = (actor: unknown) => !isBot(actor) && writers.has(login(actor) ?? ""); + const found: Signal[] = []; + const heads = new Set([pull.head, ...(carried?.heads ?? [])]); + const reviews = pull.reviews.map((review) => ({ + id: String(review.id), + url: String(review.url), + // A review names its reviewer in its body only under a bot's or a writer's account, so a forged one replaces nothing. + reviewer: isBot(review.author) || writer(review.author) ? reviewerOf(review) : null, + body: typeof review.body === "string" ? review.body : "", + at: Date.parse(String(review.submittedAt)), + commit: review.commit != null ? String(object(review.commit).oid) : "", + // A dismissed review is withdrawn: it holds nothing. + head: review.state !== "DISMISSED" && review.commit != null && heads.has(String(object(review.commit).oid)), + })); + const byId = new Map(reviews.map((review) => [review.id, review])); + const opened = new Set( + pull.reviewThreads.flatMap((thread) => { + const review = array(object(thread.comments).nodes)[0]?.pullRequestReview; + return review ? [String(object(review).id)] : []; + }), + ); + const comments = pull.comments + .map((comment) => ({ + author: comment.author, + writer: writer(comment.author), + body: String(comment.body ?? ""), + url: String(comment.url ?? ""), + at: Date.parse(String(comment.createdAt)), + })) + .sort((a, b) => a.at - b.at); + + for (const thread of pull.reviewThreads) { + if (thread.isResolved === true) continue; + const [first, ...replies] = array(object(thread.comments).nodes); + const opener = first?.pullRequestReview ? byId.get(String(object(first.pullRequestReview).id)) : undefined; + if (!opener?.reviewer || !opener.head) continue; + // A named reviewer posts under a person's account, so only its name, on the reply or the review holding it, tells + // its replies from an answer. + const own = (comment: ObjectValue) => + opener.body.startsWith("Automated review by") && + (reviewerOf(comment) === opener.reviewer || + (comment.pullRequestReview != null && + byId.get(String(object(comment.pullRequestReview).id))?.reviewer === opener.reviewer)); + if (replies.some((reply) => !own(reply) && writer(reply.author))) continue; + found.push({ + kind: "finding", + url: String(first!.url ?? opener.url), + author: opener.reviewer, + text: firstLine(String(first!.body ?? "")), + }); + } + + // Each reviewer's latest pass over each head lists what it still finds there; a pass may only open threads. A late + // pass over a carried head says nothing of another. + const latest = new Map(); + for (const review of reviews) + if (review.reviewer !== null && review.head && (review.body.trim() !== "" || opened.has(review.id))) { + const key = `${review.reviewer}\n${review.commit}`; + const prior = latest.get(key); + if (!prior || prior.at <= review.at) latest.set(key, review); + } + const passes = [...latest.values()] + .map((pass) => ({ ...pass, titles: bodyFindings(pass.body).map((finding) => finding.title), answered: false })) + .filter((pass) => pass.titles.length > 0) + .sort((a, b) => a.at - b.at); + // A writer answers a pass by linking it; an answer word alone cannot tell which finding it meant. + for (const pass of passes) + pass.answered = comments.some( + (comment) => comment.writer && comment.at >= pass.at && links(comment.body, pass.url), + ); + for (const pass of passes) + if (!pass.answered) + for (const title of pass.titles) + found.push({ kind: "finding", url: pass.url, author: pass.reviewer!, text: title }); + + const committed = Math.min(Date.parse(pull.committed), carried ? Date.parse(carried.picked) : Infinity); + for (const decision of comments) { + if (!decision.writer || !DECISION.test(decision.body) || decision.at <= committed) continue; + if (comments.some((reply) => reply.writer && reply.at > decision.at && links(reply.body, decision.url))) + continue; + found.push({ + kind: "decision", + url: decision.url, + author: login(decision.author) ?? "-", + text: firstLine(decision.body), + }); + } + return found; +} + +/** + * Which of `logins` can push to `repository`. An author association of OWNER can; MEMBER and COLLABORATOR may only + * read, so their permission is read; any other association cannot. + */ +async function writersOf( + client: GitHub, + repository: Repository, + actors: { actor: unknown; association: unknown }[], +): Promise> { + const writers = new Set(); + const asked = new Set(); + for (const { actor, association } of actors) { + const name = login(actor); + if (name === null || isBot(actor)) continue; + if (association === "OWNER") writers.add(name); + else if ((association === "MEMBER" || association === "COLLABORATOR") && !asked.has(name)) { + asked.add(name); + const response = await client.request( + `/repos/${repository.owner}/${repository.repo}/collaborators/${encodeURIComponent(name)}/permission`, + { allow: [404] }, + ); + if (response.status !== 404 && WRITE.includes(string(object(await response.json()).permission))) + writers.add(name); + } + } + return writers; +} + +/** The signals on each PR's current head, by number, with the head they were read for, and those `carried` carries. */ +export async function readSignals( + client: GitHub, + repository: Repository, + numbers: number[], + carried?: ReadonlyMap, +): Promise> { + const pulls = await readActivity(client, repository, numbers); + for (const [number, pull] of pulls) + if (pull.moved) throw new Error(`the head of PR #${number} changed while it was read; retry`); + const actors = [...pulls.values()].flatMap((pull) => [ + ...pull.reviews.map((review) => ({ actor: review.author, association: review.authorAssociation })), + ...pull.comments.map((comment) => ({ actor: comment.author, association: comment.authorAssociation })), + ...pull.reviewThreads.flatMap((thread) => + array(object(thread.comments).nodes).map((comment) => ({ + actor: comment.author, + association: comment.authorAssociation, + })), + ), + ]); + const writers = await writersOf(client, repository, actors); + return new Map( + [...pulls].map(([number, pull]) => [ + number, + { head: pull.head, signals: signals(pull, writers, carried?.get(number)) }, + ]), + ); +} + +/** Signals as one line each, for a refusal or a comment. */ +export function describe(found: Signal[]): string { + return found.map((signal) => `${signal.kind} by ${signal.author}: ${signal.text} (${signal.url})`).join("; "); +} diff --git a/src/cli/status.ts b/src/cli/status.ts index 9668769e..560f1421 100644 --- a/src/cli/status.ts +++ b/src/cli/status.ts @@ -19,6 +19,7 @@ import { appOf, checksState, contextOutcome } from "../github/checks"; import { EJECTED, LAND_CHECK, pinned } from "./land"; import { reviewSection } from "../core"; import { readHoldStatus, type HoldFact, type HoldReport } from "./hold"; +import { readSignals, type Signal } from "./signals"; /** * One query covers the PR; each connection pages by its own cursor, and a @@ -144,6 +145,8 @@ export interface Status { reactions: Reaction[]; /** Unresolved review threads, oldest first. */ threads: Thread[]; + /** What holds the head from landing: online reviewers' unanswered findings on it and newer maintainer decisions. */ + signals: Signal[]; /** Ownership facts read by the same client as the state commands. */ holds: HoldFact[]; starterHold: HoldReport["starter"]; @@ -213,6 +216,10 @@ export async function pullRequestStatus(pr: PullRequest, deps: GitHubDependencie const status = summarize(first!, rollup, current, needs, reviewNodes, threadNodes); status.reviewRecord = await reviewRecord(github, pr, first!, deps.warn); if (status.autoMerge !== null) status.autoMerge.head = await enabledHead(github, first!, deps.warn); + const signaled = (await readSignals(github, pr, [pr.number])).get(pr.number)!; + if (signaled.head !== first!.headRefOid) + throw new Error("the PR head changed while reading its signals; rerun quest status"); + status.signals = signaled.signals; return { ...status, holds: ownership.holds, starterHold: ownership.starter }; } @@ -500,6 +507,7 @@ function summarize( reviewRecord: null, reactions, threads, + signals: [], holds: [], starterHold: null, }; @@ -605,6 +613,7 @@ export function statusLines(status: Status): string[] { (t) => `thread ${t.id} ${t.outdated ? "outdated" : "current"} ${t.lastAuthor} ${t.url} ${t.path}:${dash(t.line)}`, ), + ...status.signals.map((s) => `signal ${s.kind} ${s.url} ${s.author}: ${s.text}`), ...(status.holds.length ? status.holds.map( (hold) => diff --git a/test/github.ts b/test/github.ts index 1b8d2f09..55fbf36c 100644 --- a/test/github.ts +++ b/test/github.ts @@ -333,10 +333,69 @@ function graphql(repo: Repo, query: string, variables: Record) }, }; } + // A batch of PRs' reviews, threads, and comments, as the online-review reader asks with one alias per PR. + const aliases = [...query.matchAll(/\bp(\d+): pullRequest\(number: \d+\)/g)].map((match) => Number(match[1])); + if (aliases.length) + return { data: { repository: Object.fromEntries(aliases.map((n) => [`p${n}`, activityOf(repo, n)])) } }; // Anything else reads as a repository with none of the fields asked for. return { data: { repository: {} } }; } +/** An account as GraphQL names it: a bot without its `[bot]` suffix. */ +const actorOf = (login: string | null) => + login === null + ? null + : login.endsWith("[bot]") + ? { __typename: "Bot", login: login.slice(0, -5) } + : { __typename: "User", login }; + +/** A PR's head, reviews, threads, and comments, as the online-review reader asks for them. */ +function activityOf(repo: Repo, number: number) { + const pull = repo.pulls.find((p) => p.number === number); + if (!pull) return null; + const head = headOf(repo, pull); + const url = `https://github.com/${repo.owner}/${repo.name}/pull/${number}`; + return { + headRefOid: head, + commits: page([{ commit: { oid: head, committedDate: updatedAt } }]), + reviews: page( + (pull.reviews ?? []).map((review, index) => ({ + id: `R_${number}_${index}`, + url: `${url}#pullrequestreview-${index}`, + author: actorOf(review.author), + authorAssociation: review.author === repo.owner ? "OWNER" : "COLLABORATOR", + body: review.body ?? "", + submittedAt: updatedAt, + commit: { oid: review.commit ?? head }, + })), + ), + reviewThreads: page( + (pull.threads ?? []).map((thread, index) => ({ + id: `T_${number}_${index}`, + isResolved: thread.resolved === true, + resolvedBy: null, + comments: page( + thread.comments.map((comment, at) => ({ + author: actorOf(comment.author), + body: comment.body, + url: `${url}#discussion_r${index}${at}`, + pullRequestReview: null, + })), + ), + })), + ), + comments: page( + (pull.comments ?? []).map((comment, index) => ({ + author: actorOf(comment.author), + authorAssociation: comment.author === repo.owner ? "OWNER" : "COLLABORATOR", + body: comment.body, + createdAt: comment.at, + url: `${url}#issuecomment-${index}`, + })), + ), + }; +} + function tree(repo: Repo) { const dirs = new Set(); for (const file of repo.files) { @@ -550,6 +609,8 @@ export function fakeGitHub(world: World) { return json({ data: { node: { + number: pull.number, + repository: { nameWithOwner: `${repo.owner}/${repo.name}` }, state: pull.merged ? "MERGED" : pull.closed ? "CLOSED" : "OPEN", baseRefName: pull.base ?? defaultOf(repo), headRefOid: headOf(repo, pull), diff --git a/test/hold.test.ts b/test/hold.test.ts index 03d2998f..c4e0b59c 100644 --- a/test/hold.test.ts +++ b/test/hold.test.ts @@ -45,6 +45,8 @@ class FakePull { /** Runs after each pull request read, to change it between reads. */ afterRead?: () => void; comparison = "ahead"; + /** Runs on each base comparison, the last request before landing. */ + afterCompare?: () => void; base = BASE; readonly comparisons: string[] = []; /** Check names the base branch's rulesets require. */ @@ -53,6 +55,9 @@ class FakePull { rules: ObjectValue[] = []; /** The head's check runs, as its `statusCheckRollup` lists them. */ checks: ObjectValue[] = []; + /** The PR's reviews, threads, and comments as the signal reader reads them, and its head's commit time. */ + activity: ObjectValue = {}; + committed = "2026-10-21T11:00:00Z"; /** The head's workflow runs, as `GET /actions/runs` lists them. */ runs: ObjectValue[] = []; autoMerge: ObjectValue | null = null; @@ -122,6 +127,7 @@ class FakePull { return json(200, { total_count: this.runs.length, workflow_runs: this.runs }); if (url.pathname === `/repos/example/project/compare/main...${this.head}`) { this.comparisons.push(`${url.pathname}${url.search}`); + this.afterCompare?.(); return json(200, { status: this.comparison, base_commit: { sha: this.base } }); } if (url.pathname === "/repos/example/project/rules/branches/main") { @@ -165,6 +171,23 @@ class FakePull { } return { data: { [name]: { clientMutationId: null } } }; } + if (query.includes("p7: pullRequest(number: 7)")) { + const empty = { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] }; + return { + data: { + repository: { + p7: { + headRefOid: this.head, + commits: { nodes: [{ commit: { oid: this.head, committedDate: this.committed } }] }, + reviews: empty, + reviewThreads: empty, + comments: empty, + ...this.activity, + }, + }, + }, + }; + } if (query.includes("node(id: $id)")) return { data: { node: { autoMergeRequest: this.autoMerge } } }; if (query.includes("statusCheckRollup")) return { @@ -373,6 +396,91 @@ describe("merge --land", () => { expect(pull.writes).not.toEqual([]); }); + test("refuses a head an unanswered online finding or a newer maintainer decision holds", async () => { + const pull = new FakePull(); + const empty = { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] }; + const decision = `${URL_}#issuecomment-9`; + pull.activity = { + comments: { + ...empty, + nodes: [ + { + author: { __typename: "User", login: "maintainer" }, + authorAssociation: "OWNER", + body: "Maintainer decision: add the timeout.", + createdAt: "2026-10-21T11:30:00Z", + url: decision, + }, + ], + }, + }; + expect(await refused(land(PR, { sha: HEAD, method: "merge", check: clean }, pull.deps()))).toContain( + `head ${HEAD} is held by decision by maintainer: Maintainer decision: add the timeout. (${decision})`, + ); + expect(pull.writes).toEqual([]); + // A head committed after it may apply it. + pull.committed = "2026-10-21T11:45:00Z"; + await land(PR, { sha: HEAD, method: "merge", check: clean }, pull.deps()); + expect(pull.writes).not.toEqual([]); + }); + + test("rechecks signals right before a direct merge", async () => { + const pull = new FakePull(); + pull.allowAutoMerge = false; + const empty = { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] }; + pull.afterCompare = () => { + pull.activity = { + comments: { + ...empty, + nodes: [ + { + author: { __typename: "User", login: "maintainer" }, + authorAssociation: "OWNER", + body: "Maintainer decision: late.", + createdAt: "2026-10-21T11:30:00Z", + url: `${URL_}#issuecomment-9`, + }, + ], + }, + }; + }; + expect( + await refused(land(PR, { sha: HEAD, method: "merge", direct: true, check: clean }, pull.deps())), + ).toContain("Maintainer decision: late."); + expect(pull.writes).toEqual([]); + }); + + test.each([ + ["a merge queue", (pull: FakePull) => (pull.queue = true)], + ["the quest land gate", (pull: FakePull) => (pull.required = ["quest land"])], + ])("rechecks signals after the quest check before queueing on %s", async (_, gate) => { + const pull = new FakePull(); + gate(pull); + const empty = { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] }; + // A decision posted while the quest check fetched and read the head. + const check = async () => { + pull.activity = { + comments: { + ...empty, + nodes: [ + { + author: { __typename: "User", login: "maintainer" }, + authorAssociation: "OWNER", + body: "Maintainer decision: during the check.", + createdAt: "2026-10-21T11:30:00Z", + url: `${URL_}#issuecomment-10`, + }, + ], + }, + }; + return []; + }; + expect(await refused(land(PR, { sha: HEAD, method: "merge", check }, pull.deps()))).toContain( + "Maintainer decision: during the check.", + ); + expect(pull.writes).toEqual([]); + }); + test("refuses when the token cannot see the auto-merge setting", async () => { const pull = new FakePull(); pull.allowAutoMerge = null; diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index 02116825..4e43846e 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -12,10 +12,10 @@ import type { GitHubDependencies } from "../src/cli/github"; import { readRemote } from "../src/cli/land"; import { parseResolution } from "../src/cli/land-apply"; import { CHECK_LOG } from "../src/cli/land-checks"; -import { COLLECTION_ATTEMPTS, REQUEUE_LIMIT, history } from "../src/cli/land-fixer"; +import { COLLECTION_ATTEMPTS, REQUEUE_LIMIT, carriedBy, history } from "../src/cli/land-fixer"; import { privateRepository } from "../src/cli/land-serve"; import { jobsSection, readRemoval, SETTLE, watchPass, watchPermissions } from "../src/cli/land-watch"; -import { type FixContext, HELD } from "../src/cli/land-watch-fix"; +import { endPick, type FixContext, HELD } from "../src/cli/land-watch-fix"; import { type FlakyTest, flakyName, @@ -74,6 +74,8 @@ interface Pull { /** Its head branch's pushes, oldest first, in place of those its removals imply. */ pushes?: { sha: string; at: string }[]; labels?: string[]; + /** Its reviews, threads, and comments as the signal reader reads them; none by default. */ + activity?: Record; } interface Job { id: number; @@ -200,6 +202,25 @@ function deps(world: World): GitHubDependencies { if (query.includes("defaultBranchRef")) return Response.json({ data: { repository: { defaultBranchRef: { name: "main" } } } }); const pullOf = () => world.pulls.find((item) => `PR_${item.number}` === variables.id)!; + const aliases = [...query.matchAll(/\bp(\d+): pullRequest\(number: \d+\)/g)].map((m) => Number(m[1])); + if (aliases.length) { + const empty = { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] }; + const activity = (pull: Pull) => ({ + headRefOid: headOf(world, pull), + commits: { nodes: [{ commit: { oid: headOf(world, pull), committedDate: at(-100) } }] }, + reviews: empty, + reviewThreads: empty, + comments: empty, + ...pull.activity, + }); + return Response.json({ + data: { + repository: Object.fromEntries( + aliases.map((n) => [`p${n}`, activity(world.pulls.find((pull) => pull.number === n)!)]), + ), + }, + }); + } if (query.includes("statusCheckRollup")) { expect(variables.id).toMatch(/^PR_\d+$/); const contexts = world.checks?.[variables.sha]; @@ -220,6 +241,23 @@ function deps(world: World): GitHubDependencies { return Response.json({ data: { repository: { object: { statusCheckRollup: rollup } } } }); } const mutation = /^\s*mutation[^{]*\{\s*(\w+)/.exec(query)?.[1]; + if (mutation === "dequeuePullRequest" || mutation === "disablePullRequestAutoMerge") { + const pull = pullOf(); + // Like GitHub, which refuses to disable an auto-merge request that is gone. + if ( + mutation === "disablePullRequestAutoMerge" && + pull.armed === undefined && + pull.queued === undefined + ) + return Response.json({ + errors: [{ type: "UNPROCESSABLE", message: "auto-merge is not enabled" }], + }); + world.mutations.push(`${mutation} ${pull.number}`); + // Like GitHub, leaving the queue also disarms auto-merge. + if (mutation === "dequeuePullRequest") pull.queued = undefined; + pull.armed = undefined; + return Response.json({ data: { [mutation]: { clientMutationId: null } } }); + } if (mutation === "enqueuePullRequest" || mutation === "enablePullRequestAutoMerge") { const pull = pullOf(); world.mutations.push(`${mutation} ${pull.number} ${variables.sha}`); @@ -245,6 +283,8 @@ function deps(world: World): GitHubDependencies { return Response.json({ data: { node: { + number: pull.number, + repository: { nameWithOwner: "upstream/project" }, state: pull.state ?? "OPEN", baseRefName: pull.base ?? "main", headRefOid: headOf(world, pull), @@ -747,6 +787,134 @@ test("an armed pick GitHub never queued is enqueued once ready, only at the head expect(github.mutations).toHaveLength(2); }); +test("a queued or armed pick that draws an online finding leaves the queue, once, and is not enqueued again, unless retargeted", async () => { + const state = tempDir(); + const page = (nodes: object[]) => ({ pageInfo: { hasNextPage: false, endCursor: null }, nodes }); + const finding = { + reviews: page([ + { + id: "r1", + url: "https://github.com/upstream/project/pull/30#pullrequestreview-1", + author: { __typename: "Bot", login: "rabbit" }, + body: "[P1] Drops the last frame", + submittedAt: at(3), + commit: { oid: HEAD }, + }, + ]), + }; + const github = world([ + { + number: 30, + head: HEAD, + enqueues: [{ createdAt: at(2), actor: "maintainer" }], + removals: [], + armed: at(2), + queued: at(2), + activity: finding, + }, + { number: 31, head: HEAD, enqueues: [], removals: [], armed: at(2), mergeState: "BLOCKED", activity: finding }, + // Retargeted after the list read: another base's watcher owns it now. + { number: 33, head: HEAD, enqueues: [], removals: [], armed: at(2), base: "release", activity: finding }, + // Nothing holds it. + { + number: 32, + head: HEAD, + enqueues: [{ createdAt: at(2), actor: "maintainer" }], + removals: [], + armed: at(2), + queued: at(2), + }, + ]); + github.now = Date.parse(at(4)); + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([ + `dequeued 30 ${HEAD} 1 signals`, + `disarmed 31 ${HEAD} 1 signals`, + ]); + // The dequeue disarmed #30 already, which still gets its comment. + expect(github.mutations).toEqual(["dequeuePullRequest 30", "disablePullRequestAutoMerge 31"]); + expect(github.comments[30]).toHaveLength(1); + expect(github.comments[30][0].body).toContain( + `taking it out of the queue and disarming its auto-merge, since its head ${HEAD} is held by finding by rabbit[bot]: drops the last frame`, + ); + expect(github.comments[30][0].body).toContain(""); + // Out of the queue and disarmed: the next pass leaves both alone. + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([]); + expect(github.mutations).toHaveLength(2); + expect(github.comments[31]).toHaveLength(1); + expect(github.comments[33]).toBeUndefined(); +}); + +test("a signaled pick leaves the queue even when GitHub refuses its comment, which a later pass posts", async () => { + const signaled = () => + world([ + { + number: 30, + head: HEAD, + enqueues: [{ createdAt: at(2), actor: "maintainer" }], + removals: [], + armed: at(2), + queued: at(2), + activity: { + reviews: { + pageInfo: { hasNextPage: false, endCursor: null }, + nodes: [ + { + id: "r1", + url: "https://github.com/upstream/project/pull/30#pullrequestreview-1", + author: { __typename: "Bot", login: "rabbit" }, + body: "[P1] Drops the last frame", + submittedAt: at(3), + commit: { oid: HEAD }, + }, + ], + }, + }, + }, + ]); + let state = tempDir(); + let github = signaled(); + github.now = Date.parse(at(4)); + github.refuse = ["#30 quest land watch: ended"]; + const first = await watchPass(REPOSITORY, { state }, deps(github)); + expect(first[0]).toBe(`dequeued 30 ${HEAD} 1 signals`); + expect(first[1]).toMatch(new RegExp(`^unsaid 30 ${HEAD}: `)); + expect(first).toHaveLength(2); + expect(github.mutations).toEqual(["dequeuePullRequest 30"]); + expect(github.comments[30] ?? []).toHaveLength(0); + // Out of the queue now; the kept comment still goes, once. + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([]); + expect(github.comments[30]).toHaveLength(1); + expect(github.comments[30][0].body).toContain(""); + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([]); + expect(github.comments[30]).toHaveLength(1); + expect(github.mutations).toEqual(["dequeuePullRequest 30"]); + + // A comment GitHub keeps refusing is set aside after three passes. + state = tempDir(); + github = signaled(); + github.now = Date.parse(at(4)); + github.refuse = Array(4).fill("#30 quest land watch: ended"); + expect((await watchPass(REPOSITORY, { state }, deps(github)))[1]).toMatch(/^unsaid 30 /); + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([expect.stringMatching(/^unsaid 30 /)]); + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([ + expect.stringMatching(/^set aside the signal comment on 30 after 3 refusals: /), + ]); + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([]); + expect(github.comments[30] ?? []).toHaveLength(0); + + // A kept comment whose PR closed is dropped unsaid. + state = tempDir(); + github = signaled(); + github.now = Date.parse(at(4)); + github.refuse = ["#30 quest land watch: ended"]; + await watchPass(REPOSITORY, { state }, deps(github)); + github.pulls[0].state = "CLOSED"; + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([]); + github.pulls[0].state = undefined; + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([]); + expect(github.comments[30] ?? []).toHaveLength(0); +}); + test("a refused enqueue of an armed pick is reported once and retried, and says nothing when GitHub queued it first", async () => { const state = tempDir(); const github = world([ @@ -810,7 +978,7 @@ test("an armed pick withdrawn, retargeted, or held while the pass reads it is no test("the watcher's tokens ask for writes only with a fixer, and pushes for workflows only when they carry one", () => { expect(watchPermissions(false)).toEqual({ - api: { contents: "read", pull_requests: "write", actions: "read", merge_queues: "write" }, + api: { contents: "write", pull_requests: "write", actions: "read", merge_queues: "write" }, }); expect(watchPermissions(true)).toEqual({ api: { @@ -1196,6 +1364,150 @@ describe.skipIf(process.platform === "win32")("the watcher's land fixer", () => expect(history(state, 5)!.rounds[1].rule).toBe("repeated"); }); + test("a fix whose dropped head drew a finding while the fixer ran is handed back, not re-enqueued", async () => { + const { repo, state, fix, context } = setup(); + const base = repo.main(); + const head = repo.pull(5, { "feature.txt": "new\n" }); + const pull: Pull = { + number: 5, + head, + enqueues: [{ createdAt: at(1), actor: "maintainer" }], + removals: [ + { id: "R1", createdAt: at(5), reason: "failed_checks", actor: "github-merge-queue", before: head }, + ], + }; + const github = { ...world([pull], [failedRun(5, at(3), base)]), origin: repo.origin }; + github.now = Date.parse(at(6)); + fix.round(5, fixScript("fixed.txt")); + expect(await watchPass(REPOSITORY, { state }, deps(github), context)).toEqual([ + "removed 5 failed_checks fix", + "fixing 5", + ]); + await exited(state); + // The fixer's commit read none of it, so it answers nothing on the head it replaces. + pull.activity = { + reviews: { + pageInfo: { hasNextPage: false, endCursor: null }, + nodes: [ + { + id: "r1", + url: "https://github.com/upstream/project/pull/5#pullrequestreview-1", + author: { __typename: "Bot", login: "rabbit" }, + body: "[P1] Drops the last frame", + submittedAt: at(7), + commit: { oid: head }, + }, + ], + }, + }; + github.now = Date.parse(at(10)); + expect(await watchPass(REPOSITORY, { state }, deps(github), context)).toEqual(["handback 5"]); + expect(github.mutations).toEqual([]); + expect(github.comments[5].at(-1)!.body).toContain("finding by rabbit[bot]: drops the last frame"); + // `quest status` reports why the pick stopped as the signal, not the failure the fixer took on. + expect(github.comments[5].at(-1)!.body).toContain(""); + // The handed-back fix still carries the dropped head, should anyone pick it again unanswered. + const pushed = git(repo.origin, "rev-parse", "refs/heads/branch-5"); + expect(pushed).not.toBe(head); + expect(carriedBy(state, 5, pushed)?.heads).toEqual([head]); + // Ending the pick, as a hold or a person's removal does, leaves it too. + endPick(state, 5, history(state, 5)!.pick); + expect(history(state, 5)).toBeUndefined(); + expect(carriedBy(state, 5, pushed)?.heads).toEqual([head]); + }); + + test("a fix a workflow queued and armed is dequeued once its dropped head draws a finding, without disarming twice", async () => { + const { repo, state, fix, context } = setup(); + const base = repo.main(); + const head = repo.pull(5, { "feature.txt": "new\n" }); + const pull: Pull = { + number: 5, + head, + enqueues: [{ createdAt: at(1), actor: "maintainer" }], + removals: [ + { id: "R1", createdAt: at(5), reason: "failed_checks", actor: "github-merge-queue", before: head }, + ], + }; + const github = { ...world([pull], [failedRun(5, at(3), base)]), origin: repo.origin }; + github.now = Date.parse(at(6)); + fix.round(5, fixScript("fixed.txt")); + await watchPass(REPOSITORY, { state }, deps(github), context); + await exited(state); + // A workflow queues and arms the fixer's push once it lands, before the fixer's requeue reads the PR. + github.onPull = (read) => { + const requeuing = readdirSync(state) + .filter((name) => /^watch-job-.*\.json$/.test(name)) + .some((name) => JSON.parse(readFileSync(join(state, name), "utf8")).requeuing !== undefined); + if (requeuing && read.queued === undefined && !github.mutations.length) { + read.queued = at(8); + read.armed = at(8); + } + }; + pull.activity = { + reviews: { + pageInfo: { hasNextPage: false, endCursor: null }, + nodes: [ + { + id: "r1", + url: "https://github.com/upstream/project/pull/5#pullrequestreview-1", + author: { __typename: "Bot", login: "rabbit" }, + body: "[P1] Drops the last frame", + submittedAt: at(7), + commit: { oid: head }, + }, + ], + }, + }; + github.now = Date.parse(at(10)); + expect(await watchPass(REPOSITORY, { state }, deps(github), context)).toEqual(["handback 5"]); + // Leaving the queue disarmed it, so nothing asks GitHub to disarm a request that is gone. + expect(github.mutations).toEqual(["dequeuePullRequest 5"]); + expect(github.comments[5].at(-1)!.body).toContain(""); + }); + + test("a fix queued without its finding still carries the dropped head's, and leaves the queue once one arrives", async () => { + const { repo, state, fix, context } = setup(); + const base = repo.main(); + const head = repo.pull(5, { "feature.txt": "new\n" }); + const pull: Pull = { + number: 5, + head, + enqueues: [{ createdAt: at(1), actor: "maintainer" }], + removals: [ + { id: "R1", createdAt: at(5), reason: "failed_checks", actor: "github-merge-queue", before: head }, + ], + }; + const github = { ...world([pull], [failedRun(5, at(3), base)]), origin: repo.origin }; + github.now = Date.parse(at(6)); + fix.round(5, fixScript("fixed.txt")); + await watchPass(REPOSITORY, { state }, deps(github), context); + await exited(state); + github.now = Date.parse(at(10)); + const fixed = () => git(repo.origin, "rev-parse", "refs/heads/branch-5"); + expect(await watchPass(REPOSITORY, { state }, deps(github), context)).toEqual([`requeued 5 ${fixed()}`]); + // Queued at the fix, however it got there, a finding on the head it replaced still holds it. + pull.activity = { + reviews: { + pageInfo: { hasNextPage: false, endCursor: null }, + nodes: [ + { + id: "r1", + url: "https://github.com/upstream/project/pull/5#pullrequestreview-1", + author: { __typename: "Bot", login: "rabbit" }, + body: "[P1] Drops the last frame", + submittedAt: at(11), + commit: { oid: head }, + }, + ], + }, + }; + github.now = Date.parse(at(12)); + expect(await watchPass(REPOSITORY, { state }, deps(github), context)).toEqual([ + `dequeued 5 ${fixed()} 1 signals`, + ]); + expect(github.mutations.at(-1)).toBe("dequeuePullRequest 5"); + }); + test("a fix that edits a protected path is handed back unpushed, as a ready commit", async () => { const { repo, state, fix, context } = setup(); const base = repo.main(); diff --git a/test/land.test.ts b/test/land.test.ts index 7a9de696..70daf6cc 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -27,13 +27,16 @@ import { ADMITTED, author, COLLECTION_ATTEMPTS, + carriedBy, collectFixers, validateFix, fixerMarkdown, fixerText, REQUEUE_LIMIT, history, + historyPath, redactTokens, + saveRound, type Round, stopReason, stopRule, @@ -862,6 +865,32 @@ describe.skipIf(process.platform === "win32")("quest land", () => { expect((await serveBatch(root, resumed, options, deps, async () => deps.token))[0]).toMatch(/^landed 1 /); }); + test("a prepared head still carries a finding on the pin it merged the base into", async () => { + const forge = new Forge(); + const pull = forge.pull("Old", { "old.txt": "yes" }); + forge.advance({ "new.txt": "yes" }); + const deps = await forge.deps(); + const plan = await resolveQueue(REPOSITORY, SQUASH, deps); + const root = forge.clone(); + const options = sandbox(); + const link = (await build(root, plan)).links[0]; + await prepareHead( + root, + options.state, + plan, + plan.queue[0], + plan.base.sha, + link.tree, + deps, + async () => deps.token, + ); + pull.reviews = [ + { state: "COMMENTED", author: "rabbit[bot]", body: "[P1] Drops a frame", commit: plan.queue[0].head }, + ]; + const resumed = await resolvePreparedQueue(root, options.state, REPOSITORY, SQUASH, deps); + expect(resumed.eject.map((ejected) => [ejected.number, ejected.reason])).toEqual([[1, "unanswered_signal"]]); + }); + test("a stale queue head reuses the daemon's recorded pushed merge", async () => { const forge = new Forge(); forge.pull("Old", { "old.txt": "yes" }); @@ -1418,6 +1447,21 @@ describe.skipIf(process.platform === "win32")("quest land", () => { expect(plan.waiting?.map(({ number }) => number)).toEqual([2, 4]); }); + test("ejects a pick whose head draws an unanswered online finding", async () => { + const forge = new Forge(); + const runs = [{ name: "test", conclusion: "SUCCESS", url: "u" }]; + forge.pull("Ready", { "1.txt": "1\n" }, { pull: { runs } }); + forge.pull( + "Found", + { "2.txt": "2\n" }, + { pull: { runs, reviews: [{ state: "COMMENTED", author: "rabbit[bot]", body: "[P1] Drops a frame" }] } }, + ); + const plan = await resolveQueue(REPOSITORY, { base: "main", method: "squash" }, await forge.deps()); + expect(plan.queue.map((entry) => entry.number)).toEqual([1]); + expect(plan.eject.map((ejected) => [ejected.number, ejected.reason])).toEqual([[2, "unanswered_signal"]]); + expect(plan.eject[0]!.detail).toContain("finding by rabbit[bot]: drops a frame"); + }); + test("refuses classic protection settings it does not uphold, or cannot read", async () => { const forge = new Forge(); forge.pull("Ready", { "1.txt": "1\n" }); @@ -3191,6 +3235,22 @@ fi`); { outcome: "handback", rule: "repeated", before: fixed }, ]); }); + test("hands back a fix whose dropped head drew a finding while the fixer ran", async () => { + const fixture = await fixerFixture(fixedScript(), { conflict: true }); + await fixture.finished; + // The fixer's commit read none of it, so it answers nothing on the head it replaces. + fixture.pull.reviews = [ + { + state: "COMMENTED", + author: "rabbit[bot]", + body: "[P1] Drops a frame", + commit: fixture.plan.queue[0].head, + }, + ]; + expect(await fixture.collect()).toEqual(["handback 1"]); + expect(fixture.pull.landing).toBe(false); + expect(fixture.forge.comment(1)).toContain("finding by rabbit[bot]: drops a frame"); + }); test("accepts a fix whose base gained protected changes before collection", async () => { const fixture = await fixerFixture(fixedScript(), { conflict: true, protectedBase: true }); await fixture.finished; @@ -3409,6 +3469,23 @@ fi`); // Collateral is never a round of its pick, however often it shares a broken batch. expect(history(fixture.state.state, 2)!.rounds).toEqual([]); }); + test("a fix of collateral, which records no round, still carries the head it replaced", async () => { + const fixture = await fixerFixture( + `git checkout -q pr-2 +printf 'tidier\\n' > other.txt +git add other.txt +git commit -qm 'tidy' +printf '[{"number":1,"action":"handback","diagnosis":"breaks the check","recommendation":"run quest-iterate","blame":true,"log":[1,1]},{"number":2,"action":"fixed","head":"%s","reason":"tidy"}]' "$(git rev-parse HEAD)" > quest-land-result.json`, + { collateral: true }, + ); + await fixture.finished; + const before = fixture.collateral!.head!; + expect(await fixture.collect()).toEqual(["handback 1", "requeued 2"]); + const after = fixture.collateral!.head!; + expect(after).not.toBe(before); + expect(history(fixture.state.state, 2)!.rounds).toEqual([]); + expect(carriedBy(fixture.state.state, 2, after)?.heads).toEqual([before]); + }); test("a PR merged while the fixer ran is left alone, and the rest of its batch is still collected", async () => { const fixture = await fixerFixture( `printf '[{"number":1,"action":"handback","diagnosis":"breaks the check","recommendation":"run quest-iterate","blame":true,"log":[1,1]},{"number":2,"action":"requeue","reason":"collateral: shared the batch with 1"}]' > quest-land-result.json`, @@ -4559,6 +4636,46 @@ describe("land fixer stop rules", () => { }); }); +test("a fixed head carries the heads its pick's rounds replaced", () => { + const state = tempDir(); + const round = (job: string, before: string, after?: string, pin?: string): Round => ({ + job, + outcome: after ? "fixed" : "handback", + before, + failure: "f", + ...(after ? { after } : {}), + ...(pin ? { pin } : {}), + }); + const pick = "2026-11-10T17:00:00Z"; + expect(carriedBy(state, 5, "b")).toBeUndefined(); + let record = saveRound(state, 5, { pick, rounds: [] }, round("1", "m", "b", "a")); + record = saveRound(state, 5, record, round("2", "b", "c")); + expect(carriedBy(state, 5, "b")).toEqual({ heads: ["m", "a"], picked: pick }); + expect(carriedBy(state, 5, "c")).toEqual({ heads: ["m", "a", "b"], picked: pick }); + // A head no round put back, such as a writer's push, carries nothing. + expect(carriedBy(state, 5, "d")).toBeUndefined(); + // A handback that drops `after`, even re-saving the round that set it, and the history's end leave it. + record = saveRound(state, 5, record, round("2", "b")); + rmSync(historyPath(state, 5)); + expect(carriedBy(state, 5, "c")).toEqual({ heads: ["m", "a", "b"], picked: pick }); + // A later pick of the carried head keeps the earlier pick's time and heads, and never rewrites the record. + const later = "2026-11-11T17:00:00Z"; + record = saveRound(state, 5, { pick: later, rounds: [] }, round("3", "c", "e")); + expect(carriedBy(state, 5, "e")).toEqual({ heads: ["m", "a", "b", "c"], picked: pick }); + saveRound(state, 5, { pick: later, rounds: [] }, round("4", "x", "c")); + expect(carriedBy(state, 5, "c")).toEqual({ heads: ["m", "a", "b"], picked: pick }); + // A gate merge on a carried head, whether fixed again or requeued unchanged, keeps its pin's carry. + saveRound(state, 5, { pick: later, rounds: [] }, round("7", "m2", "n", "e")); + saveRound(state, 5, { pick: later, rounds: [] }, round("8", "m3", "m3", "e")); + expect(carriedBy(state, 5, "n")).toEqual({ heads: ["m", "a", "b", "c", "m2", "e"], picked: pick }); + expect(carriedBy(state, 5, "m3")).toEqual({ heads: ["m", "a", "b", "c", "e"], picked: pick }); + // An unchanged requeue carries nothing new, and a pick that is not a time cannot bound decisions. + saveRound(state, 5, { pick, rounds: [] }, round("5", "f", "f")); + saveRound(state, 5, { pick: "f".repeat(40), rounds: [] }, round("6", "g", "h")); + expect(carriedBy(state, 5, "f")).toBeUndefined(); + expect(carriedBy(state, 5, "h")).toBeUndefined(); +}); + /** The test commit the own queue pushed for PR `number`, if its branch exists. */ function queued(forge: Forge, number: number): string | null { const found = spawnSync("git", ["-C", forge.origin, "rev-parse", `refs/heads/${queueBranch("main", number)}`], { @@ -4910,6 +5027,39 @@ describe.skipIf(process.platform === "win32")("the watcher's own queue", () => { expect(await pass(0, fix)).toEqual(["landed 2"]); expect(await pass(0, fix)).toEqual(["building 1"]); }); + + test("the land fixer's requeue hands back a head a finding drew while it ran", async () => { + const { forge, queue, pass } = setup(); + const one = forge.pull("One", { "one.txt": "1\n" }); + const tools = tempDir(); + const wrapper = join(tools, "wrapper"); + writeFileSync( + wrapper, + `#!/bin/sh\nset -eu\nprintf '[{"number":%s,"action":"requeue","reason":"healthy","failure":"just test: render","cause":"flaky"}]' "$2" > quest-land-result.json\n`, + { mode: 0o755 }, + ); + const fix: FixContext = { + root: queue.root, + fixer: wrapper, + jobs: 1, + effort: "medium", + branches: [], + tags: [], + timeout: 60, + token: queue.token, + read: readRemote, + }; + await pass(0, fix); + report(forge, 1, "FAILURE", "flaked\n"); + const finished = eventFile(queue.state, (name) => name.endsWith(".exit")); + expect(await pass(0, fix)).toEqual(["removed 1", "fixing 1"]); + await finished; + one.reviews = [{ state: "COMMENTED", author: "rabbit[bot]", body: "[P1] Drops a frame" }]; + expect(await pass(0, fix)).toEqual(["handback 1"]); + expect(one.landing).toBeFalsy(); + expect(one.comments?.at(-1)?.body).toContain("finding by rabbit[bot]: drops a frame"); + expect(one.comments?.at(-1)?.body).toContain(""); + }); }); test("the watcher picks its queue from the base's rules each pass", async () => { diff --git a/test/online-review.test.ts b/test/online-review.test.ts index 25faf4e7..f4c66d98 100644 --- a/test/online-review.test.ts +++ b/test/online-review.test.ts @@ -1,6 +1,8 @@ import { describe, expect, test } from "vitest"; -import { type Activity, bodyFindings, classify, onlineReviews } from "../src/cli/online-review"; +import type { GitHub, Repository } from "../src/cli/github"; +import { type Activity, bodyFindings, classify, onlineReviews, readActivity } from "../src/cli/online-review"; +import { readSignals } from "../src/cli/signals"; const HEAD = "h".repeat(40); const bot = (login: string) => ({ __typename: "Bot", login }); @@ -21,6 +23,7 @@ const thread = (comments: object[], resolvedBy: object | null = null) => ({ }); const activity = (pull: Partial): Activity => ({ head: HEAD, + committed: "2026-11-06T00:00:00Z", reviews: [], reviewThreads: [], comments: [], @@ -40,7 +43,10 @@ describe("classify", () => { describe("bodyFindings", () => { test("two findings that share a title in one body stay two", () => { - expect(bodyFindings("[P1] Guard null (GET)\n[P2] Guard null (POST)")).toEqual(["guard null", "guard null #2"]); + expect(bodyFindings("[P1] Guard null (GET)\n[P2] Guard null (POST)").map((finding) => finding.key)).toEqual([ + "guard null", + "guard null #2", + ]); }); test("reads bracketed and dashed markers, keyed by title", () => { @@ -53,7 +59,7 @@ describe("bodyFindings", () => { "The previous P2 is fixed.", "P2 is a prose word here.", ].join("\n"); - expect(bodyFindings(body)).toEqual([ + expect(bodyFindings(body).map((finding) => finding.key)).toEqual([ "keep sampling when unavailable", "src/b.ts:10–12", "restore the hierarchy", @@ -156,3 +162,86 @@ describe("onlineReviews", () => { ]); }); }); + +test("a head pushed while later pages are read marks the read moved, which fails a signal read", async () => { + const page = (nodes: object[], next: boolean) => ({ + pageInfo: { hasNextPage: next, endCursor: next ? "1" : null }, + nodes, + }); + const client = (moved: boolean) => + ({ + graphql: async (query: string) => { + if (query.includes("$reviews")) + return { + repository: { + pullRequest: { + headRefOid: moved ? "m".repeat(40) : HEAD, + reviews: page([review("r2", bot("rabbit"), "[P1] New", "2026-11-10T17:06:00Z")], false), + }, + }, + }; + return { + repository: { + p1: { + headRefOid: HEAD, + commits: { nodes: [{ commit: { oid: HEAD, committedDate: "2026-11-10T17:00:00Z" } }] }, + reviews: page([review("r1", bot("rabbit"), "Ok.", "2026-11-10T17:05:00Z")], true), + reviewThreads: page([], false), + comments: page([], false), + }, + }, + }; + }, + }) as unknown as GitHub; + const repository = { owner: "acme", repo: "proj" } as Repository; + const still = (await readActivity(client(false), repository, [1])).get(1)!; + expect([still.reviews.length, still.moved]).toEqual([2, undefined]); + // Usage tallies take the mixed read; a signal read, which judges findings by the head, refuses it. + expect((await readActivity(client(true), repository, [1])).get(1)!.moved).toBe(true); + await expect(readSignals(client(true), repository, [1])).rejects.toThrow("head of PR #1 changed while it was read"); + expect((await readSignals(client(false), repository, [1])).get(1)!.signals.map((signal) => signal.text)).toEqual([ + "new", + ]); +}); + +test("a head pushed while a long thread's later comments are read marks the read moved", async () => { + const read = async (moved: boolean) => { + const client = { + graphql: async (query: string) => + query.includes("PullRequestReviewThread") + ? { + node: { + pullRequest: { headRefOid: moved ? "m".repeat(40) : HEAD }, + comments: { pageInfo: { hasNextPage: false }, nodes: [{ body: "Later." }] }, + }, + } + : { + repository: { + p1: { + headRefOid: HEAD, + commits: { + nodes: [{ commit: { oid: HEAD, committedDate: "2026-11-10T17:00:00Z" } }], + }, + reviews: { pageInfo: { hasNextPage: false }, nodes: [] }, + reviewThreads: { + pageInfo: { hasNextPage: false }, + nodes: [ + { + id: "t1", + comments: { + pageInfo: { hasNextPage: true, endCursor: "1" }, + nodes: [{ body: "First." }], + }, + }, + ], + }, + comments: { pageInfo: { hasNextPage: false }, nodes: [] }, + }, + }, + }, + } as unknown as GitHub; + return (await readActivity(client, { owner: "acme", repo: "proj" } as Repository, [1])).get(1)!.moved; + }; + expect(await read(false)).toBeUndefined(); + expect(await read(true)).toBe(true); +}); diff --git a/test/signals.test.ts b/test/signals.test.ts new file mode 100644 index 00000000..06ef3481 --- /dev/null +++ b/test/signals.test.ts @@ -0,0 +1,263 @@ +import { describe as group, expect, test } from "vitest"; + +import type { Activity } from "../src/cli/online-review"; +import { signals } from "../src/cli/signals"; + +const HEAD = "h".repeat(40); +const OLD = "o".repeat(40); +const URL_ = "https://github.com/acme/proj/pull/1"; +const WRITERS = new Set(["maintainer"]); +const bot = (login: string) => ({ __typename: "Bot", login }); +const person = { __typename: "User", login: "maintainer" }; +const reader = { __typename: "User", login: "drive-by" }; +const review = (id: string, author: object, body: string, at: string, commit = HEAD) => ({ + id, + url: `${URL_}#pullrequestreview-${id}`, + author, + body, + submittedAt: at, + commit: { oid: commit }, +}); +const reply = (author: object, body: string, review: string | null, id = "c") => ({ + author, + body, + url: `${URL_}#discussion_r${id}`, + pullRequestReview: review === null ? null : { id: review }, +}); +const thread = (comments: object[], resolved = false) => ({ + isResolved: resolved, + comments: { pageInfo: { hasNextPage: false }, nodes: comments }, +}); +const comment = (author: object, body: string, at: string, id: string) => ({ + author, + body, + createdAt: at, + url: `${URL_}#issuecomment-${id}`, +}); +const activity = (pull: Partial): Activity => ({ + head: HEAD, + committed: "2026-11-10T17:00:00Z", + reviews: [], + reviewThreads: [], + comments: [], + ...pull, +}); + +group("signals", () => { + test("an online reviewer's thread on the head holds it until a writer replies or it is resolved", () => { + const pull = activity({ + reviews: [ + review("r1", bot("rabbit"), "Actionable comments posted: 3", "2026-11-10T17:05:00Z"), + review("r2", bot("rabbit"), "Old pass.", "2026-11-10T16:00:00Z", OLD), + ], + reviewThreads: [ + thread([ + reply(bot("rabbit"), "\n\n** Minor**\n\nLeaks the handle.", "r1", "1"), + ]), + thread([reply(bot("rabbit"), "Answered.", "r1", "2"), reply(person, "Declined: closed below.", null)]), + thread([reply(bot("rabbit"), "Resolved.", "r1", "3")], true), + // A bot's reply, or one by an account that cannot push, answers nothing. + thread([reply(bot("rabbit"), "Botted.", "r1", "4"), reply(bot("frog"), "Agreed.", null)]), + thread([reply(bot("rabbit"), "Drive-by.", "r1", "5"), reply(reader, "Fixed.", null)]), + // On an earlier head: a push since answers it. + thread([reply(bot("rabbit"), "Stale.", "r2", "6")]), + // A person's own review is no online reviewer's. + thread([reply(person, "Hmm.", null, "7")]), + ], + }); + expect(signals(pull, WRITERS).map((signal) => [signal.url, signal.text])).toEqual([ + [`${URL_}#discussion_r1`, "Minor"], + [`${URL_}#discussion_r4`, "Botted."], + [`${URL_}#discussion_r5`, "Drive-by."], + ]); + }); + + test("a named reviewer's own replies under a writer's account answer nothing", () => { + const named = "Automated review by review (OpenAI)\n\nOne thread."; + const pull = activity({ + reviews: [ + review("r1", person, named, "2026-11-10T17:05:00Z"), + review("r2", person, named, "2026-11-10T17:06:00Z"), + ], + reviewThreads: [ + thread([reply(person, "Bug.", "r1", "1"), reply(person, "Still.", "r2")]), + // A reply posted alone sits in an empty-bodied review, so its own body names the reviewer. + thread([ + reply(person, "Bug.", "r1", "2"), + reply(person, "Automated review by review (OpenAI)\n\nStill not fixed.", "r3"), + ]), + ], + }); + pull.reviews.push(review("r3", person, "", "2026-11-10T17:07:00Z")); + expect(signals(pull, WRITERS)).toHaveLength(2); + }); + + test("a body finding on the head holds it until a writer's later comment links that pass", () => { + const body = [ + "Automated review by review (OpenAI)", + "", + "1. **[P2] Share handover credit across predecessors** — [rs/a.rs:311](https://x). Long.", + "2. **[P3] Name the timeout**", + ].join("\n"); + const pull = activity({ reviews: [review("r1", person, body, "2026-11-10T17:05:00Z")] }); + expect(signals(pull, WRITERS).map((signal) => [signal.author, signal.text])).toEqual([ + ["review (OpenAI)", "share handover credit across predecessors"], + ["review (OpenAI)", "name the timeout"], + ]); + // Before the pass, by a bot or a reader, or linking nothing: no answer. + pull.comments = [ + comment(person, "Fixed in abc.", "2026-11-10T17:01:00Z", "1"), + comment(bot("watcher"), `Declined, see ${URL_}#pullrequestreview-r1`, "2026-11-10T17:06:00Z", "2"), + comment(reader, "Declined: not a bug.", "2026-11-10T17:06:00Z", "3"), + comment(person, "Moot after merging main.", "2026-11-10T17:07:00Z", "4"), + ]; + expect(signals(pull, WRITERS)).toHaveLength(2); + pull.comments.push( + comment(person, `See ${URL_}#pullrequestreview-r1: both refuted.`, "2026-11-10T17:08:00Z", "5"), + ); + expect(signals(pull, WRITERS)).toEqual([]); + // An answer word alone cannot tell which finding it meant. + pull.comments.pop(); + pull.comments.push(comment(person, "Declined: the caller already checks.", "2026-11-10T17:08:00Z", "5")); + expect(signals(pull, WRITERS)).toHaveLength(2); + }); + + test("an answer clears only the passes it links, even once a linked pass is superseded", () => { + const pull = activity({ + reviews: [ + review("a", bot("rabbit"), "[P1] Guard null", "2026-11-10T17:05:00Z"), + review("b", bot("frog"), "[P2] Name it", "2026-11-10T17:06:00Z"), + ], + comments: [ + comment(person, `Declined ${URL_}#pullrequestreview-a: checked above.`, "2026-11-10T17:07:00Z", "1"), + ], + }); + expect(signals(pull, WRITERS).map((signal) => signal.text)).toEqual(["name it"]); + pull.reviews.push(review("c", bot("rabbit"), "No findings.", "2026-11-10T17:08:00Z")); + expect(signals(pull, WRITERS).map((signal) => signal.text)).toEqual(["name it"]); + }); + + test("a reviewer's latest pass over the head is the one that holds it", () => { + const pull = activity({ + reviews: [ + review("r1", bot("rabbit"), "[P1] Guard null", "2026-11-10T17:05:00Z"), + review("r2", bot("rabbit"), "No findings.", "2026-11-10T17:10:00Z"), + review("r3", bot("frog"), "[P2] Old head", "2026-11-10T16:00:00Z", OLD), + ], + }); + expect(signals(pull, WRITERS)).toEqual([]); + }); + + test("a later pass that only opens threads supersedes an earlier body's findings", () => { + const pull = activity({ + reviews: [ + review("r1", bot("rabbit"), "[P1] Guard null", "2026-11-10T17:05:00Z"), + review("r2", bot("rabbit"), "", "2026-11-10T17:10:00Z"), + review("r3", bot("rabbit"), "", "2026-11-10T17:11:00Z"), + ], + // r3 only replies, so it is no pass. + reviewThreads: [ + thread([reply(bot("rabbit"), "New.", "r2", "1"), reply(bot("rabbit"), "More.", "r3")], true), + ], + }); + expect(signals(pull, WRITERS)).toEqual([]); + pull.reviewThreads = [ + thread([reply(bot("rabbit"), "Reply only.", "r1", "1"), reply(bot("rabbit"), "More.", "r3")], true), + ]; + expect(signals(pull, WRITERS).map((signal) => signal.text)).toEqual(["guard null"]); + }); + + test("a writer's marked decision newer than the head's commit holds it until a writer links it", () => { + const decision = comment( + person, + " **Maintainer decision** (2026-11-10): raise the timeout.\n\nDetails.", + "2026-11-10T17:12:00Z", + "9", + ); + const pull = activity({ + comments: [ + decision, + // Before the head's commit: the head may apply it. + comment(person, "Maintainer decision: older.", "2026-11-10T16:00:00Z", "1"), + // Not a writer's, quoted, or not opening with the marker. + comment(bot("rabbit"), "Maintainer decision: bot.", "2026-11-10T17:13:00Z", "2"), + comment(reader, "Maintainer decision: me.", "2026-11-10T17:13:00Z", "3"), + comment(person, "> Maintainer decision: quoted.\n\nApplied.", "2026-11-10T17:13:00Z", "4"), + comment(person, "```\nMaintainer decision: fenced.\n```", "2026-11-10T17:13:00Z", "10"), + comment(person, "`Maintainer decision` lines hold a head.", "2026-11-10T17:13:00Z", "11"), + comment(person, " Maintainer decision: indented code.", "2026-11-10T17:13:00Z", "12"), + comment(person, "\tMaintainer decision: tabbed code.", "2026-11-10T17:13:00Z", "13"), + comment(person, "Per the Maintainer decision, ...", "2026-11-10T17:13:00Z", "5"), + ], + }); + expect(signals(pull, WRITERS)).toEqual([ + { + kind: "decision", + url: `${URL_}#issuecomment-9`, + author: "maintainer", + text: "Maintainer decision (2026-11-10): raise the timeout.", + }, + ]); + // A head committed after it answers it, as does a writer's later comment linking it; a bot's or reader's does not. + expect(signals({ ...pull, committed: "2026-11-10T17:20:00Z" }, WRITERS)).toEqual([]); + pull.comments.push(comment(bot("watcher"), `Held by ${URL_}#issuecomment-9`, "2026-11-10T17:14:00Z", "6")); + pull.comments.push(comment(reader, `Done: ${URL_}#issuecomment-9`, "2026-11-10T17:14:00Z", "7")); + expect(signals(pull, WRITERS)).toHaveLength(1); + pull.comments.push(comment(person, `Covered by the head: ${URL_}#issuecomment-9`, "2026-11-10T17:15:00Z", "8")); + expect(signals(pull, WRITERS)).toEqual([]); + }); + + test("a named review counts only under a bot's or a writer's account", () => { + const pull = activity({ + reviews: [ + review("r1", person, "Automated review by review (OpenAI)\n\n[P1] Guard null", "2026-11-10T17:05:00Z"), + review("r2", reader, "Automated review by review (OpenAI)\n\nNo findings.", "2026-11-10T17:06:00Z"), + ], + }); + expect(signals(pull, WRITERS).map((signal) => signal.text)).toEqual(["guard null"]); + }); + + test("a head that carries a pick forward keeps the pick's findings and the decisions since it", () => { + const pull = activity({ + committed: "2026-11-10T18:00:00Z", + reviews: [review("r1", bot("rabbit"), "[P1] Guard null", "2026-11-10T17:30:00Z", OLD)], + comments: [ + comment(person, "Maintainer decision: before the pick.", "2026-11-10T16:30:00Z", "1"), + comment(person, "Maintainer decision: while it ran.", "2026-11-10T17:40:00Z", "2"), + ], + }); + expect(signals(pull, WRITERS)).toEqual([]); + const carried = { heads: [OLD], picked: "2026-11-10T17:00:00Z" }; + expect(signals(pull, WRITERS, carried).map((signal) => [signal.kind, signal.text])).toEqual([ + ["finding", "guard null"], + ["decision", "Maintainer decision: while it ran."], + ]); + }); + + test("a late pass over a carried head leaves the current head's findings, and a dismissed review holds nothing", () => { + const pull = activity({ + reviews: [ + review("r1", bot("rabbit"), "[P1] Guard null\n[P2] Guard null", "2026-11-10T17:30:00Z"), + review("r2", bot("rabbit"), "No findings.", "2026-11-10T17:40:00Z", OLD), + { ...review("r3", bot("frog"), "[P1] Leaks", "2026-11-10T17:35:00Z"), state: "DISMISSED" }, + ], + }); + const carried = { heads: [OLD], picked: "2026-11-10T17:00:00Z" }; + expect(signals(pull, WRITERS, carried).map((signal) => signal.text)).toEqual(["guard null", "guard null"]); + }); + + test("an answer links a whole anchor, not the start of a longer one", () => { + const pull = activity({ + reviews: [review("1", bot("rabbit"), "[P1] Guard null", "2026-11-10T17:05:00Z")], + comments: [ + comment(person, "Maintainer decision: add it.", "2026-11-10T17:06:00Z", "9"), + comment(person, `See ${URL_}#pullrequestreview-12 and #issuecomment-90.`, "2026-11-10T17:07:00Z", "2"), + ], + }); + expect(signals(pull, WRITERS)).toHaveLength(2); + pull.comments.push( + comment(person, `See #pullrequestreview-1, and #issuecomment-9.`, "2026-11-10T17:08:00Z", "3"), + ); + expect(signals(pull, WRITERS)).toEqual([]); + }); +}); diff --git a/test/status.test.ts b/test/status.test.ts index c38be319..d5704f2c 100644 --- a/test/status.test.ts +++ b/test/status.test.ts @@ -118,6 +118,8 @@ function setup( warnings?: string[]; /** The base-to-head diff per compared head, or the status GitHub refuses it with. */ diffs?: Record; + /** The PR's reviews, threads, and comments as the signal reader reads them; none by default. */ + signals?: Node; /** The base's active ruleset rules. */ rules?: Node[]; /** The base branch is gone, so reading it fails. */ @@ -179,6 +181,25 @@ function setup( return Response.json(page.rows, { headers: page.next ? { Link: `<${page.next}>; rel="next"` } : {} }); } const request = JSON.parse(String(init?.body)); + if (request.query.includes("p1: pullRequest(number: 1)")) { + if (!last) throw new Error("signals read before PR state"); + const head = last.data.repository.pullRequest.headRefOid; + const empty = { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] }; + return Response.json({ + data: { + repository: { + p1: { + headRefOid: head, + commits: { nodes: [{ commit: { oid: head, committedDate: "2026-10-16T00:00:00Z" } }] }, + reviews: empty, + reviewThreads: empty, + comments: empty, + ...options.signals, + }, + }, + }, + }); + } if (request.query.includes("query HoldOwnership(")) { holdQueries.push(request.variables); if (!last) throw new Error("ownership read before PR state"); @@ -280,6 +301,30 @@ test("reports checks by requiredness, each reviewer's latest review against the ]); }); +test("lists the signals that hold the head: unanswered online findings and newer maintainer decisions", async () => { + const body = pull({ checks: null, reviews: page([]), threads: page([]) }); + const decision = `${URL_}#issuecomment-9`; + const status = await pullRequestStatus( + pr, + setup([body], { + signals: { + comments: page([ + { + author: { __typename: "User", login: "kixelated" }, + authorAssociation: "OWNER", + body: "Maintainer decision: raise the timeout.", + createdAt: "2026-10-16T01:00:00Z", + url: decision, + }, + ]), + }, + }).deps, + ); + expect(statusLines(status)).toContain( + `signal decision ${decision} kixelated: Maintainer decision: raise the timeout.`, + ); +}); + test("a queued PR with auto-merge and no checks reports both, and JSON is deterministic", async () => { const body = pull({ state: "OPEN", diff --git a/test/usage.test.ts b/test/usage.test.ts index f04208a5..9f0bb9aa 100644 --- a/test/usage.test.ts +++ b/test/usage.test.ts @@ -545,6 +545,7 @@ describe("usage", () => { // #10's final head got a bot's review with one thread, which the agent fixed. const activity = (number: number) => ({ headRefOid: "b".repeat(40), + commits: connection([{ commit: { oid: "b".repeat(40), committedDate: "2026-11-06T14:00:00Z" } }]), reviews: connection( number === 10 ? [