From d2910120ec25f724c45a05a03263fb79532cbfe6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 11 Nov 2026 22:51:05 -0800 Subject: [PATCH 01/21] chore(quest): claim quest/m0/late-signals.md Quest-Claim: 172405d3610735e9 From 16ad0e5793d94dedcc79df4e64495e7273c3cd2d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 11 Nov 2026 23:18:05 -0800 Subject: [PATCH 02/21] feat(merge): hold a head an unanswered finding or a newer maintainer decision signals quest merge --land refuses a head that an online reviewer's unanswered finding on it, or a writer's comment opening with "Maintainer decision" posted after its commit, holds, naming each. quest status lists them as signal lines. quest land's queue ejects such a pick as unanswered_signal, and quest land watch takes one out of GitHub's merge queue and disarms its auto-merge. The guide's Coordination section has coordinators post the user's answers to a PR's decisions in such a comment. Finishes quest/m0/late-signals.md. Co-Authored-By: Claude Opus 5.5 --- README.md | 30 +++++++- assets/AGENTS.md | 1 + quest/m0/late-signals.md | 57 --------------- quest/m0/mistake-audit.md | 4 +- src/cli/hold.ts | 16 ++++ src/cli/land-watch.ts | 55 ++++++++++++++ src/cli/land.ts | 14 +++- src/cli/main.ts | 3 +- src/cli/online-review.ts | 39 +++++++--- src/cli/signals.ts | 135 ++++++++++++++++++++++++++++++++++ src/cli/status.ts | 9 +++ test/github.ts | 58 +++++++++++++++ test/hold.test.ts | 48 ++++++++++++ test/land-watch.test.ts | 85 +++++++++++++++++++++ test/land.test.ts | 15 ++++ test/signals.test.ts | 150 ++++++++++++++++++++++++++++++++++++++ test/status.test.ts | 45 ++++++++++++ test/usage.test.ts | 1 + 18 files changed, 690 insertions(+), 75 deletions(-) delete mode 100644 quest/m0/late-signals.md create mode 100644 src/cli/signals.ts create mode 100644 test/signals.test.ts diff --git a/README.md b/README.md index 7e90c258..9205fcb7 100644 --- a/README.md +++ b/README.md @@ -499,6 +499,7 @@ comes last, and a missing value prints as `-`: | `review` | `current` or `stale` against the head, state, reviewer, time, and URL of each reviewer's latest review. | | `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. | @@ -512,7 +513,7 @@ is one branch protection or a ruleset requires for this PR. `--json` prints the same facts as one object with the fields `url`, `state`, `draft`, `head`, `mergeable`, `mergeState`, `reviewDecision`, `autoMerge`, `mergeQueue`, `landingStopped`, `checks` (`state`, `passed`, `failing`, `pending`), `reviews`, `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. @@ -553,6 +554,8 @@ it creates is the queue signal, so a queued PR shows no hold. It refuses while any session holds the PR, so release your own hold first. `--land` needs the full 40-character head SHA and refuses once the head moves, or while the latest run of any check on it failed, required or not, naming each; pending checks do not block. +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 @@ -856,6 +859,27 @@ 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 that arrives 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 someone else replies; a `[P]` line in its latest + review body over the head holds until a later comment links that review or + opens with an answering word such as `Fixed` or `Declined`. +- `decision`: a comment by an account with write access that opens with + `Maintainer decision`, posted after the head's commit, which therefore cannot + apply it. A newer commit answers it, as does a later comment by a writer + that links it. + +A bot's comments answer nothing. + ## 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, @@ -867,7 +891,9 @@ 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. 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 diff --git a/assets/AGENTS.md b/assets/AGENTS.md index e9a7d5c9..a9f1d99e 100644 --- a/assets/AGENTS.md +++ b/assets/AGENTS.md @@ -193,6 +193,7 @@ A long-running coordinator lets compaction bound its context: it keeps what must Start each sub-agent in the background with its own isolated worktree, since agents cannot write to a worktree their parent created. It 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 the brief of each agent started after, and report it once. A report that is an API error, such as a rate limit or an overload, or a cancellation the user did not ask for, is not a result. 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 a82ab787..6e4a3927 100644 --- a/quest/m0/mistake-audit.md +++ b/quest/m0/mistake-audit.md @@ -109,7 +109,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. @@ -127,7 +127,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 3be78153..85b22803 100644 --- a/src/cli/hold.ts +++ b/src/cli/hold.ts @@ -20,6 +20,7 @@ import { latestContexts } from "../github/checks"; import { ALLOWS, LAND_CHECK, METHOD_NAMES, contexts, enqueue } from "./land"; import { QUEUE_CHECK } from "./land-queue"; import { classifyCheck } from "./status"; +import { describe, readSignals } from "./signals"; import { Refusal, ago } from "./start"; export type Method = "merge" | "squash" | "rebase"; @@ -241,6 +242,7 @@ export async function land( } }; await passing(labels.github, pr, snapshot.url, sha); + await unsignaled(labels.github, pr, snapshot.url, sha); const needs = await requirements(labels.github, pr, snapshot.base); if (needs.checks.some((check) => check.name === LAND_CHECK)) { if (named !== undefined) @@ -337,6 +339,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 reply to each finding and link each 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-watch.ts b/src/cli/land-watch.ts index bfe41ac9..4f03b2bd 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -44,6 +44,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"; @@ -313,6 +314,59 @@ 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, pulls: ObjectValue[]): Promise { + const picked = pulls.filter((pull) => pull.mergeQueueEntry != null || pull.autoMergeRequest != null); + if (picked.length === 0) return []; + const output: string[] = []; + const read = await readSignals( + github, + repository, + picked.map((pull) => Number(pull.number)), + ); + 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.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; + if (now.mergeQueueEntry != null) + await github.graphql("mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", { + id: pull.id, + }); + if (now.autoMergeRequest != null) + await github.graphql( + "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", + { id: pull.id }, + ); + await post( + github, + { repository: `${repository.owner}/${repository.repo}` }, + { number, enabledAt: pick }, + `quest land watch: took this pull request out of the queue at ${head}, which is held by ${describe(found.signals)}. Push a fix, or answer each, then land it again.`, + `signaled ${head}`, + "quest land watch", + "unanswered_signal", + ); + output.push(`dequeued ${number} ${head} ${found.signals.length} signals`); + } + 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[] = []; @@ -690,6 +744,7 @@ export async function watchPass( save(path, progress); } } + output.push(...(await dequeueSignaled(github, repository, pulls))); 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(); diff --git a/src/cli/land.ts b/src/cli/land.ts index 8d495439..17a14a22 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -18,6 +18,7 @@ import { } from "./github"; import { appOf, latestContexts } from "../github/checks"; import { classifyCheck } from "./status"; +import { describe, readSignals } from "./signals"; import type { Method } from "./hold"; import { committedSources, snapshot } from "./tree"; @@ -108,7 +109,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; @@ -388,6 +390,11 @@ 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)), + ); for (const node of landing) { const request = object(node.autoMergeRequest); const head = string(node.headRefOid); @@ -423,6 +430,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, 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 59a89c83..63f6e48c 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -613,7 +613,8 @@ export function main(argv: string[], out: Output): number | Promise { "once when nothing is left to wait for. It takes no label: the auto-merge or merge-queue entry it " + "creates is the queue signal. It refuses while a session holds the PR, so release your own hold " + "first, once the head has moved, and while the latest run of any check on the head failed, required " + - "or not, naming each; a pending or absent check does not block. It uses the method --squash or --rebase names, refusing one " + + "or not, naming each; a pending or absent check does not block; and 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 " + diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index 3f64eb12..bdbcefa0 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -30,16 +30,21 @@ 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 + * 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 keyed by its * title and how many earlier lines of the body share that title. */ export function bodyFindings(body: string): 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(); @@ -49,13 +54,13 @@ export function bodyFindings(body: string): string[] { }); } -const THREAD_COMMENT = "author { __typename login } body pullRequestReview { id }"; +const THREAD_COMMENT = "author { __typename login } body url pullRequestReview { id }"; const FIELDS = { reviews: "id url author { __typename login } 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 +70,20 @@ 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. */ +export type Activity = Record & { head: string; committed?: string }; /** 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 +96,9 @@ 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 commit = object(object(array(object(pull.commits).nodes)[0] ?? {}).commit ?? {}); const nodes = { head: String(pull.headRefOid) } as Activity; + if (commit.oid === pull.headRefOid) nodes.committed = String(commit.committedDate); const cursors: Partial> = {}; for (const name of CONNECTIONS) { const connection = object(pull[name]); @@ -142,7 +153,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 +161,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; @@ -242,6 +256,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 +287,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..66133bbc --- /dev/null +++ b/src/cli/signals.ts @@ -0,0 +1,135 @@ +// What holds a PR's head from landing besides its checks: an online reviewer's finding on the head that nobody has +// answered, and a maintainer decision recorded in a comment after the head was committed that nothing has answered. +import type { GitHub, ObjectValue, Repository } from "./github"; +import { array, object } from "./github"; +import { type Activity, bodyFindings, classify, 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 marker a comment opens with to record a maintainer decision, after any markup such as `**`. */ +export const DECISION = /^[^A-Za-z]*Maintainer decision\b/i; + +/** Comment author associations of accounts with write access, whose decisions and answers count. */ +const WRITERS = ["OWNER", "MEMBER", "COLLABORATOR"]; + +/** A body's first line of prose, past markup such as a bot's HTML comments, cut short. */ +const firstLine = (body: string) => + body + .replace(/|<[^>\n]*>/g, "") + .split("\n") + .map((line) => line.replace(/\*\*\s*\*\*/g, "").trim()) + .find((line) => /[A-Za-z]/.test(line)) + ?.slice(0, 120) ?? ""; + +/** + * The signals holding `pull`'s head. A finding is an online reviewer's, from its latest pass over the head: a review + * thread it opened that is unresolved with no reply from anyone else, or a `[P]` line of that pass's body that no + * later comment answers, by linking the pass or taking a side in its first word as `online-review` reads answers. A + * decision is a comment by a writer 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. + */ +export function signals(pull: Activity): Signal[] { + const found: Signal[] = []; + const reviews = pull.reviews.map((review) => ({ + id: String(review.id), + url: String(review.url), + reviewer: reviewerOf(review), + author: login(review.author) ?? "-", + body: typeof review.body === "string" ? review.body : "", + at: Date.parse(String(review.submittedAt)), + head: review.commit != null && String(object(review.commit).oid) === pull.head, + })); + const byId = new Map(reviews.map((review) => [review.id, review])); + const comments = [...pull.comments] + .map((comment) => ({ + author: comment.author, + writer: WRITERS.includes(String(comment.authorAssociation)) && !isBot(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 own reviews tell its replies from an answer. + const own = (comment: ObjectValue) => + opener.body.startsWith("Automated review by") + ? comment.pullRequestReview != null && + byId.get(String(object(comment.pullRequestReview).id))?.reviewer === opener.reviewer + : login(comment.author) === opener.reviewer; + if (replies.some((reply) => !own(reply))) 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 the head lists what it still finds there. + const latest = new Map(); + for (const review of reviews) + if (review.reviewer !== null && review.head && review.body.trim() !== "") { + const prior = latest.get(review.reviewer); + if (!prior || prior.at <= review.at) latest.set(review.reviewer, review); + } + const reviewers = new Set(reviews.flatMap((review) => (review.reviewer === null ? [] : [review.reviewer]))); + for (const pass of latest.values()) { + const titles = bodyFindings(pass.body); + if (!titles.length) continue; + const anchor = pass.url.slice(pass.url.lastIndexOf("#")); + const answered = comments.some( + (comment) => + comment.at >= pass.at && + !isBot(comment.author) && + !reviewers.has(login(comment.author) ?? "") && + (comment.body.includes(anchor) || classify(comment.body) !== "unclassified"), + ); + if (answered) continue; + for (const title of titles) found.push({ kind: "finding", url: pass.url, author: pass.reviewer!, text: title }); + } + + const committed = pull.committed === undefined ? Number.NaN : Date.parse(pull.committed); + for (const decision of comments) { + if (!decision.writer || !DECISION.test(decision.body)) continue; + // An unknown commit time cannot show the head applied it. + if (decision.at <= committed) continue; + const anchor = decision.url.slice(decision.url.lastIndexOf("#")); + if (comments.some((reply) => reply.writer && reply.at > decision.at && reply.body.includes(anchor))) continue; + found.push({ + kind: "decision", + url: decision.url, + author: login(decision.author) ?? "-", + text: firstLine(decision.body), + }); + } + return found; +} + +/** The signals on each PR's current head, by number, with the head they were read for. */ +export async function readSignals( + client: GitHub, + repository: Repository, + numbers: number[], +): Promise> { + const pulls = await readActivity(client, repository, numbers); + return new Map([...pulls].map(([number, pull]) => [number, { head: pull.head, signals: signals(pull) }])); +} + +/** 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 d4c84dd0..ee1b35c7 100644 --- a/src/cli/status.ts +++ b/src/cli/status.ts @@ -15,6 +15,7 @@ import { checksState, contextOutcome, latestContexts } from "../github/checks"; import { EJECTED, 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 @@ -138,6 +139,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"]; @@ -200,6 +203,10 @@ export async function pullRequestStatus(pr: PullRequest, deps: GitHubDependencie const status = summarize(first!, rollup, contexts, 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 }; } @@ -423,6 +430,7 @@ function summarize( reviewRecord: null, reactions, threads, + signals: [], holds: [], starterHold: null, }; @@ -494,6 +502,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 b6b1a51e..cb165273 100644 --- a/test/github.ts +++ b/test/github.ts @@ -305,10 +305,68 @@ 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), + 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: "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) { diff --git a/test/hold.test.ts b/test/hold.test.ts index e6dcd502..3d8e0b03 100644 --- a/test/hold.test.ts +++ b/test/hold.test.ts @@ -43,6 +43,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"; autoMerge: ObjectValue | null = null; queueEntry: ObjectValue | null = null; labels: string[] = []; @@ -151,6 +154,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 { @@ -331,6 +351,34 @@ 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, HEAD, "merge", 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, HEAD, "merge", pull.deps()); + expect(pull.writes).not.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 6bfa40db..7e225c10 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -72,6 +72,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; @@ -198,6 +200,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]; @@ -218,6 +239,13 @@ 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(); + world.mutations.push(`${mutation} ${pull.number}`); + if (mutation === "dequeuePullRequest") pull.queued = undefined; + else 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}`); @@ -741,6 +769,63 @@ 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", 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 }, + // 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`, + `dequeued 31 ${HEAD} 1 signals`, + ]); + expect(github.mutations).toEqual([ + "dequeuePullRequest 30", + "disablePullRequestAutoMerge 30", + "disablePullRequestAutoMerge 31", + ]); + expect(github.comments[30]).toHaveLength(1); + expect(github.comments[30][0].body).toContain( + `took this pull request out of the queue at ${HEAD}, which 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(3); + expect(github.comments[31]).toHaveLength(1); +}); + 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([ diff --git a/test/land.test.ts b/test/land.test.ts index 26f0e732..323902f8 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -1214,6 +1214,21 @@ describe.skipIf(process.platform === "win32")("quest land", () => { expect(plan.waiting?.map(({ number }) => number)).toEqual([2]); }); + 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" }); diff --git a/test/signals.test.ts b/test/signals.test.ts new file mode 100644 index 00000000..4dac0286 --- /dev/null +++ b/test/signals.test.ts @@ -0,0 +1,150 @@ +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 bot = (login: string) => ({ __typename: "Bot", login }); +const person = { __typename: "User", login: "maintainer" }; +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, association = "OWNER") => ({ + author, + authorAssociation: association, + 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 someone else replies or resolves it", () => { + 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"), "Leaks the handle.\nMore.", "r1", "1")]), + thread([reply(bot("rabbit"), "Answered.", "r1", "2"), reply(person, "Declined: closed below.", null)]), + thread([reply(bot("rabbit"), "Resolved.", "r1", "3")], true), + // On an earlier head: a push since answers it. + thread([reply(bot("rabbit"), "Stale.", "r2", "4")]), + // A person's own review is no online reviewer's. + thread([reply(person, "Hmm.", null, "5")]), + ], + }); + expect(signals(pull)).toEqual([ + { kind: "finding", url: `${URL_}#discussion_r1`, author: "rabbit[bot]", text: "Leaks the handle." }, + ]); + }); + + test("a body finding on the head holds it until a later comment answers or links its 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).map((signal) => [signal.author, signal.text])).toEqual([ + ["review (OpenAI)", "share handover credit across predecessors"], + ["review (OpenAI)", "name the timeout"], + ]); + // A comment before the pass, a bot's, or one that takes no side and links nothing answers nothing. + 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(person, "Moot after merging main.", "2026-11-10T17:07:00Z", "3"), + ]; + expect(signals(pull)).toHaveLength(2); + pull.comments.push( + comment(person, `See ${URL_}#pullrequestreview-r1: both refuted.`, "2026-11-10T17:08:00Z", "4"), + ); + expect(signals(pull)).toEqual([]); + pull.comments.pop(); + pull.comments.push(comment(person, "Declined: the caller already checks.", "2026-11-10T17:08:00Z", "4")); + expect(signals(pull)).toEqual([]); + }); + + 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)).toEqual([]); + }); + + 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, or not opening with the marker. + comment(bot("rabbit"), "Maintainer decision: bot.", "2026-11-10T17:13:00Z", "2", "NONE"), + comment( + { __typename: "User", login: "drive-by" }, + "Maintainer decision: me.", + "2026-11-10T17:13:00Z", + "3", + "NONE", + ), + comment(person, "Per the Maintainer decision, ...", "2026-11-10T17:13:00Z", "4"), + ], + }); + expect(signals(pull)).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 link does not. + expect(signals({ ...pull, committed: "2026-11-10T17:20:00Z" })).toEqual([]); + pull.comments.push( + comment(bot("watcher"), `Held by ${URL_}#issuecomment-9`, "2026-11-10T17:14:00Z", "5", "NONE"), + ); + expect(signals(pull)).toHaveLength(1); + pull.comments.push(comment(person, `Covered by the head: ${URL_}#issuecomment-9`, "2026-11-10T17:15:00Z", "6")); + expect(signals(pull)).toEqual([]); + // An unknown commit time cannot show the head applied either decision. + pull.comments.pop(); + expect(signals({ ...pull, committed: undefined })).toHaveLength(2); + }); +}); diff --git a/test/status.test.ts b/test/status.test.ts index ba7a8f83..4a55516a 100644 --- a/test/status.test.ts +++ b/test/status.test.ts @@ -114,6 +114,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; } = {}, ) { const queries: Record[] = []; @@ -156,6 +158,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"); @@ -248,6 +269,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 17908863..f08cba2d 100644 --- a/test/usage.test.ts +++ b/test/usage.test.ts @@ -410,6 +410,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 ? [ From f284a2c0a522ccc7fdce928565614f8c86635f7c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 11 Nov 2026 23:49:47 -0800 Subject: [PATCH 03/21] fix(signals): only writers answer, answers target their review, and the fixer's requeue checks signals Co-Authored-By: Claude Opus 5.5 --- README.md | 23 +++---- assets/skills/iterate.md | 1 + assets/skills/merge.md | 2 +- assets/skills/run.md | 2 +- src/cli/land-watch-fix.ts | 10 ++- src/cli/land-watch.ts | 34 +++++++--- src/cli/main.ts | 5 +- src/cli/online-review.ts | 9 +-- src/cli/signals.ts | 128 ++++++++++++++++++++++++------------- test/github.ts | 2 + test/land-watch.test.ts | 4 +- test/land.test.ts | 31 +++++++++ test/online-review.test.ts | 1 + test/signals.test.ts | 109 +++++++++++++++++++------------ 14 files changed, 246 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 9205fcb7..be91e41d 100644 --- a/README.md +++ b/README.md @@ -865,20 +865,21 @@ 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 that arrives before the merge -ends the pick. There are two kinds: +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 someone else replies; a `[P]` line in its latest - review body over the head holds until a later comment links that review or - opens with an answering word such as `Fixed` or `Declined`. -- `decision`: a comment by an account with write access that opens with - `Maintainer decision`, posted after the head's commit, which therefore cannot - apply it. A newer commit answers it, as does a later comment by a writer - that links it. - -A bot's comments answer nothing. + until it is resolved or a writer replies. A `[P]` line in its latest + review body over the head holds until a writer's later comment links that + review, or, linking none, opens with an answering word such as `Fixed` or + `Declined`, which answers the latest such review before it. +- `decision`: a writer's comment that opens with `Maintainer decision`, not in + a quote, 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. + +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 diff --git a/assets/skills/iterate.md b/assets/skills/iterate.md index 35218a4f..27dd6be7 100644 --- a/assets/skills/iterate.md +++ b/assets/skills/iterate.md @@ -21,6 +21,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 decisions beyond its follow-ups' need the user, run `/quest-review` with your token, which reviews the branch, pushes, and marks the PR ready. diff --git a/assets/skills/merge.md b/assets/skills/merge.md index a3e0462e..749285eb 100644 --- a/assets/skills/merge.md +++ b/assets/skills/merge.md @@ -16,7 +16,7 @@ If a `hold` record shows another session holds the PR, stop and report it; takin The head is landable when: - a `## Review` record that counts names it, or the exemptions below cover the changes since the one it names; -- 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; - 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. A head on which any check failed, required or not, needs `/quest-iterate` to fix it or rerun the check, flaky ones included; `quest merge` refuses it, naming each failing check. diff --git a/assets/skills/run.md b/assets/skills/run.md index bdc946ae..ac3e349f 100644 --- a/assets/skills/run.md +++ b/assets/skills/run.md @@ -57,7 +57,7 @@ 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` or `failed_checks` the pick holds, even from an earlier session: dispatch `/quest-iterate`. After `checks_pending`, run `/quest-merge` again once they pass. +Without one, after `merge_conflict`, `failed_checks`, or `unanswered_signal` the pick holds, even from an earlier session: dispatch `/quest-iterate`. After `checks_pending`, run `/quest-merge` again once they pass. Any other reason, such as a person removing it, is the user's call. ## Loop diff --git a/src/cli/land-watch-fix.ts b/src/cli/land-watch-fix.ts index 3679484d..a234ff30 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, queueHead } from "../github/merge"; import { UNREFUSED } from "./hold"; +import { 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"; @@ -121,7 +122,7 @@ const JUDGED: Record = { }; const PULL = `query($id: ID!) { node(id: $id) { ... on PullRequest { - state baseRefName headRefOid mergeStateStatus mergeQueueEntry { enqueuedAt } autoMergeRequest { enabledAt } + number repository { nameWithOwner } state baseRefName headRefOid mergeStateStatus mergeQueueEntry { enqueuedAt } autoMergeRequest { enabledAt } labels(first: 100) { nodes { name } } headRepository { nameWithOwner } } } }`; @@ -722,6 +723,13 @@ async function requeue( if (string(pull.headRefOid) !== head) throw new FixerRefusal("head moved before it was re-enqueued"); if (!queued(pull, dropped)) { if (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. + const [owner, repo] = string(object(pull.repository).nameWithOwner).split("/"); + const held = (await readSignals(github, { owner: owner!, repo: repo! }, [Number(pull.number)])).get( + Number(pull.number), + )!; + if (held.head !== head) throw new FixerRefusal("head moved before it was re-enqueued"); + if (held.signals.length > 0) throw new FixerRefusal(`its head is held by ${describe(held.signals)}`); 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 4f03b2bd..0c662707 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -344,20 +344,34 @@ async function dequeueSignaled(github: GitHub, repository: Repository, pulls: Ob ? string(object(now.mergeQueueEntry).enqueuedAt) : null; if (pick === null) continue; - if (now.mergeQueueEntry != null) - await github.graphql("mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", { - id: pull.id, - }); - if (now.autoMergeRequest != null) - await github.graphql( - "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", - { id: pull.id }, - ); + try { + if (now.mergeQueueEntry != null) + await github.graphql( + "mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", + { id: pull.id }, + ); + if (now.autoMergeRequest != null) + await github.graphql( + "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", + { id: pull.id }, + ); + } catch (error) { + if (!(error instanceof GitHubGraphQLError)) throw error; + // GitHub moved first: it merged, closed, or left the queue, or its head moved. The next pass reads it again. + const fresh = await readPull(github, string(pull.id)); + if ( + fresh.state !== "OPEN" || + string(fresh.headRefOid) !== head || + (fresh.mergeQueueEntry == null && fresh.autoMergeRequest == null) + ) + continue; + throw error; + } await post( github, { repository: `${repository.owner}/${repository.repo}` }, { number, enabledAt: pick }, - `quest land watch: took this pull request out of the queue at ${head}, which is held by ${describe(found.signals)}. Push a fix, or answer each, then land it again.`, + `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, since its head ${head} is held by ${describe(found.signals)}. Push a fix, or answer each, then land it again.`, `signaled ${head}`, "quest land watch", "unanswered_signal", diff --git a/src/cli/main.ts b/src/cli/main.ts index 63f6e48c..aadfa163 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -543,7 +543,8 @@ export function main(argv: string[], out: Output): number | Promise { "actor, and time, until the PR enters the merge queue or auto-merge again. Then " + "a check line per failing or pending check, marked required or optional, counting only each check's latest run; a review line per " + "reviewer's latest review, marked current or stale against the head; 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.", @@ -742,7 +743,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. " + `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 bdbcefa0..d982c0cb 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -54,7 +54,7 @@ export function bodyFindings(body: string): string[] { }); } -const THREAD_COMMENT = "author { __typename login } body url 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 }", reviewThreads: @@ -71,7 +71,7 @@ const page = (name: Connection, after: string) => const BATCH = 10; /** A PR's last head and its committer date, and its reviews, review threads, and comments as GitHub's GraphQL API gives them. */ -export type Activity = Record & { head: string; committed?: string }; +export type Activity = Record & { head: string; committed: string }; /** Each PR's reviews, review threads, and comments, and its head, read in batches and then in pages. */ export async function readActivity( @@ -97,8 +97,9 @@ export async function readActivity( throw new Error(`GitHub has no PR #${number} in ${repository.owner}/${repository.repo} for this token`); const pull = object(value); const commit = object(object(array(object(pull.commits).nodes)[0] ?? {}).commit ?? {}); - const nodes = { head: String(pull.headRefOid) } as Activity; - if (commit.oid === pull.headRefOid) nodes.committed = String(commit.committedDate); + 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]); diff --git a/src/cli/signals.ts b/src/cli/signals.ts index 66133bbc..b949ad19 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -1,7 +1,6 @@ -// What holds a PR's head from landing besides its checks: an online reviewer's finding on the head that nobody has +// 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, ObjectValue, Repository } from "./github"; -import { array, object } from "./github"; +import { type GitHub, type ObjectValue, type Repository, array, object, string } from "./github"; import { type Activity, bodyFindings, classify, 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. */ @@ -15,44 +14,48 @@ export interface Signal { text: string; } -/** The marker a comment opens with to record a maintainer decision, after any markup such as `**`. */ -export const DECISION = /^[^A-Za-z]*Maintainer decision\b/i; +/** The marker a comment opens with to record a maintainer decision, after markup such as `**` but not in a quote. */ +export const DECISION = /^[^A-Za-z>]*Maintainer decision\b/i; -/** Comment author associations of accounts with write access, whose decisions and answers count. */ -const WRITERS = ["OWNER", "MEMBER", "COLLABORATOR"]; +/** 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, cut short. */ +/** 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(/\*\*\s*\*\*/g, "").trim()) + .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("#")); + /** - * The signals holding `pull`'s head. A finding is an online reviewer's, from its latest pass over the head: a review - * thread it opened that is unresolved with no reply from anyone else, or a `[P]` line of that pass's body that no - * later comment answers, by linking the pass or taking a side in its first word as `online-review` reads answers. A - * decision is a comment by a writer 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. + * 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 answers. A comment answers each pass it links, + * else, when its first word takes a side as `online-review` reads answers, the latest pass before it with findings. 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. */ -export function signals(pull: Activity): Signal[] { +export function signals(pull: Activity, writers: ReadonlySet): Signal[] { + const writer = (actor: unknown) => !isBot(actor) && writers.has(login(actor) ?? ""); const found: Signal[] = []; const reviews = pull.reviews.map((review) => ({ id: String(review.id), url: String(review.url), reviewer: reviewerOf(review), - author: login(review.author) ?? "-", body: typeof review.body === "string" ? review.body : "", at: Date.parse(String(review.submittedAt)), head: review.commit != null && String(object(review.commit).oid) === pull.head, })); const byId = new Map(reviews.map((review) => [review.id, review])); - const comments = [...pull.comments] + const comments = pull.comments .map((comment) => ({ author: comment.author, - writer: WRITERS.includes(String(comment.authorAssociation)) && !isBot(comment.author), + writer: writer(comment.author), body: String(comment.body ?? ""), url: String(comment.url ?? ""), at: Date.parse(String(comment.createdAt)), @@ -66,11 +69,10 @@ export function signals(pull: Activity): Signal[] { if (!opener?.reviewer || !opener.head) continue; // A named reviewer posts under a person's account, so only its own reviews tell its replies from an answer. const own = (comment: ObjectValue) => - opener.body.startsWith("Automated review by") - ? comment.pullRequestReview != null && - byId.get(String(object(comment.pullRequestReview).id))?.reviewer === opener.reviewer - : login(comment.author) === opener.reviewer; - if (replies.some((reply) => !own(reply))) continue; + opener.body.startsWith("Automated review by") && + 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), @@ -86,28 +88,30 @@ export function signals(pull: Activity): Signal[] { const prior = latest.get(review.reviewer); if (!prior || prior.at <= review.at) latest.set(review.reviewer, review); } - const reviewers = new Set(reviews.flatMap((review) => (review.reviewer === null ? [] : [review.reviewer]))); - for (const pass of latest.values()) { - const titles = bodyFindings(pass.body); - if (!titles.length) continue; - const anchor = pass.url.slice(pass.url.lastIndexOf("#")); - const answered = comments.some( - (comment) => - comment.at >= pass.at && - !isBot(comment.author) && - !reviewers.has(login(comment.author) ?? "") && - (comment.body.includes(anchor) || classify(comment.body) !== "unclassified"), - ); - if (answered) continue; - for (const title of titles) found.push({ kind: "finding", url: pass.url, author: pass.reviewer!, text: title }); + const passes = [...latest.values()] + .map((pass) => ({ ...pass, titles: bodyFindings(pass.body), answered: false })) + .filter((pass) => pass.titles.length > 0) + .sort((a, b) => a.at - b.at); + for (const comment of comments) { + if (!comment.writer) continue; + const linked = passes.filter((pass) => comment.at >= pass.at && comment.body.includes(anchorOf(pass.url))); + const targets = + linked.length > 0 + ? linked + : classify(comment.body) === "unclassified" + ? [] + : passes.filter((pass) => pass.at <= comment.at).slice(-1); + for (const pass of targets) pass.answered = true; } + 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 = pull.committed === undefined ? Number.NaN : Date.parse(pull.committed); + const committed = Date.parse(pull.committed); for (const decision of comments) { - if (!decision.writer || !DECISION.test(decision.body)) continue; - // An unknown commit time cannot show the head applied it. - if (decision.at <= committed) continue; - const anchor = decision.url.slice(decision.url.lastIndexOf("#")); + if (!decision.writer || !DECISION.test(decision.body) || decision.at <= committed) continue; + const anchor = anchorOf(decision.url); if (comments.some((reply) => reply.writer && reply.at > decision.at && reply.body.includes(anchor))) continue; found.push({ kind: "decision", @@ -119,6 +123,34 @@ export function signals(pull: Activity): Signal[] { 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. */ export async function readSignals( client: GitHub, @@ -126,7 +158,17 @@ export async function readSignals( numbers: number[], ): Promise> { const pulls = await readActivity(client, repository, numbers); - return new Map([...pulls].map(([number, pull]) => [number, { head: pull.head, signals: signals(pull) }])); + const actors = [...pulls.values()].flatMap((pull) => [ + ...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) }])); } /** Signals as one line each, for a refusal or a comment. */ diff --git a/test/github.ts b/test/github.ts index cb165273..e6cc70fb 100644 --- a/test/github.ts +++ b/test/github.ts @@ -573,6 +573,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/land-watch.test.ts b/test/land-watch.test.ts index 7e225c10..4333e285 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -271,6 +271,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), @@ -817,7 +819,7 @@ test("a queued or armed pick that draws an online finding leaves the queue, once ]); expect(github.comments[30]).toHaveLength(1); expect(github.comments[30][0].body).toContain( - `took this pull request out of the queue at ${HEAD}, which is held by finding by rabbit[bot]: drops the last frame`, + `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. diff --git a/test/land.test.ts b/test/land.test.ts index 323902f8..e3d71c08 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -4678,6 +4678,37 @@ 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", + 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"); + }); }); 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..9e288f04 100644 --- a/test/online-review.test.ts +++ b/test/online-review.test.ts @@ -21,6 +21,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: [], diff --git a/test/signals.test.ts b/test/signals.test.ts index 4dac0286..8d9f130f 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -6,8 +6,10 @@ 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}`, @@ -26,9 +28,8 @@ const thread = (comments: object[], resolved = false) => ({ isResolved: resolved, comments: { pageInfo: { hasNextPage: false }, nodes: comments }, }); -const comment = (author: object, body: string, at: string, id: string, association = "OWNER") => ({ +const comment = (author: object, body: string, at: string, id: string) => ({ author, - authorAssociation: association, body, createdAt: at, url: `${URL_}#issuecomment-${id}`, @@ -43,28 +44,47 @@ const activity = (pull: Partial): Activity => ({ }); group("signals", () => { - test("an online reviewer's thread on the head holds it until someone else replies or resolves it", () => { + 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"), "Leaks the handle.\nMore.", "r1", "1")]), + 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", "4")]), + thread([reply(bot("rabbit"), "Stale.", "r2", "6")]), // A person's own review is no online reviewer's. - thread([reply(person, "Hmm.", null, "5")]), + thread([reply(person, "Hmm.", null, "7")]), ], }); - expect(signals(pull)).toEqual([ - { kind: "finding", url: `${URL_}#discussion_r1`, author: "rabbit[bot]", text: "Leaks the handle." }, + 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 body finding on the head holds it until a later comment answers or links its pass", () => { + 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")])], + }); + expect(signals(pull, WRITERS)).toHaveLength(1); + }); + + test("a body finding on the head holds it until a writer's later comment answers that pass", () => { const body = [ "Automated review by review (OpenAI)", "", @@ -72,24 +92,40 @@ group("signals", () => { "2. **[P3] Name the timeout**", ].join("\n"); const pull = activity({ reviews: [review("r1", person, body, "2026-11-10T17:05:00Z")] }); - expect(signals(pull).map((signal) => [signal.author, signal.text])).toEqual([ + expect(signals(pull, WRITERS).map((signal) => [signal.author, signal.text])).toEqual([ ["review (OpenAI)", "share handover credit across predecessors"], ["review (OpenAI)", "name the timeout"], ]); - // A comment before the pass, a bot's, or one that takes no side and links nothing answers nothing. + // Before the pass, by a bot or a reader, or taking no side and 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(person, "Moot after merging main.", "2026-11-10T17:07:00Z", "3"), + 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)).toHaveLength(2); + expect(signals(pull, WRITERS)).toHaveLength(2); pull.comments.push( - comment(person, `See ${URL_}#pullrequestreview-r1: both refuted.`, "2026-11-10T17:08:00Z", "4"), + comment(person, `See ${URL_}#pullrequestreview-r1: both refuted.`, "2026-11-10T17:08:00Z", "5"), ); - expect(signals(pull)).toEqual([]); + expect(signals(pull, WRITERS)).toEqual([]); pull.comments.pop(); - pull.comments.push(comment(person, "Declined: the caller already checks.", "2026-11-10T17:08:00Z", "4")); - expect(signals(pull)).toEqual([]); + pull.comments.push(comment(person, "Declined: the caller already checks.", "2026-11-10T17:08:00Z", "5")); + expect(signals(pull, WRITERS)).toEqual([]); + }); + + test("an answer clears only the passes it links, else the latest before it", () => { + 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.comments = [comment(person, "Declined: checked above.", "2026-11-10T17:07:00Z", "1")]; + expect(signals(pull, WRITERS).map((signal) => signal.text)).toEqual(["guard null"]); }); test("a reviewer's latest pass over the head is the one that holds it", () => { @@ -100,7 +136,7 @@ group("signals", () => { review("r3", bot("frog"), "[P2] Old head", "2026-11-10T16:00:00Z", OLD), ], }); - expect(signals(pull)).toEqual([]); + expect(signals(pull, WRITERS)).toEqual([]); }); test("a writer's marked decision newer than the head's commit holds it until a writer links it", () => { @@ -115,36 +151,27 @@ group("signals", () => { 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, or not opening with the marker. - comment(bot("rabbit"), "Maintainer decision: bot.", "2026-11-10T17:13:00Z", "2", "NONE"), - comment( - { __typename: "User", login: "drive-by" }, - "Maintainer decision: me.", - "2026-11-10T17:13:00Z", - "3", - "NONE", - ), - comment(person, "Per the Maintainer decision, ...", "2026-11-10T17:13:00Z", "4"), + // 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, "Per the Maintainer decision, ...", "2026-11-10T17:13:00Z", "5"), ], }); - expect(signals(pull)).toEqual([ + expect(signals(pull, WRITERS)).toEqual([ { kind: "decision", url: `${URL_}#issuecomment-9`, author: "maintainer", - text: "**Maintainer decision** (2026-11-10): raise the timeout.", + 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 link does not. - expect(signals({ ...pull, committed: "2026-11-10T17:20:00Z" })).toEqual([]); - pull.comments.push( - comment(bot("watcher"), `Held by ${URL_}#issuecomment-9`, "2026-11-10T17:14:00Z", "5", "NONE"), - ); - expect(signals(pull)).toHaveLength(1); - pull.comments.push(comment(person, `Covered by the head: ${URL_}#issuecomment-9`, "2026-11-10T17:15:00Z", "6")); - expect(signals(pull)).toEqual([]); - // An unknown commit time cannot show the head applied either decision. - pull.comments.pop(); - expect(signals({ ...pull, committed: undefined })).toHaveLength(2); + // 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([]); }); }); From 35984ac384f93b06da5c8615eb1a3ccfed3f4201 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 00:03:54 -0800 Subject: [PATCH 04/21] fix(signals): a body finding is answered only by a writer's link to its review; say a dequeue that disarmed Co-Authored-By: Claude Opus 5.5 --- README.md | 3 +-- src/cli/land-watch.ts | 22 ++++++++++++++-------- src/cli/signals.ts | 21 +++++++-------------- test/land-watch.test.ts | 12 +++++------- test/signals.test.ts | 13 +++++++------ 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index be91e41d..11c595f8 100644 --- a/README.md +++ b/README.md @@ -872,8 +872,7 @@ merge ends the pick. There are two kinds: `Automated review by `) on the head. A review thread it opened holds until it is resolved or a writer replies. A `[P]` line in its latest review body over the head holds until a writer's later comment links that - review, or, linking none, opens with an answering word such as `Fixed` or - `Declined`, which answers the latest such review before it. + review. - `decision`: a writer's comment that opens with `Maintainer decision`, not in a quote, 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. diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 0c662707..4e66acfe 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -344,28 +344,34 @@ async function dequeueSignaled(github: GitHub, repository: Repository, pulls: Ob ? string(object(now.mergeQueueEntry).enqueuedAt) : null; if (pick === null) continue; + // 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 moved first and neither was taken, the next pass reads it again. + let taken = false; try { - if (now.mergeQueueEntry != null) + if (now.mergeQueueEntry != null) { await github.graphql( "mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", { id: pull.id }, ); - if (now.autoMergeRequest != null) + taken = true; + } + const armed = taken ? (await readPull(github, string(pull.id))).autoMergeRequest : now.autoMergeRequest; + if (armed != null) { await github.graphql( "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", { id: pull.id }, ); + taken = true; + } } catch (error) { if (!(error instanceof GitHubGraphQLError)) throw error; - // GitHub moved first: it merged, closed, or left the queue, or its head moved. The next pass reads it again. const fresh = await readPull(github, string(pull.id)); - if ( + const ended = fresh.state !== "OPEN" || string(fresh.headRefOid) !== head || - (fresh.mergeQueueEntry == null && fresh.autoMergeRequest == null) - ) - continue; - throw error; + (fresh.mergeQueueEntry == null && fresh.autoMergeRequest == null); + if (!ended) throw error; + if (!taken) continue; } await post( github, diff --git a/src/cli/signals.ts b/src/cli/signals.ts index b949ad19..90f1f654 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -1,7 +1,7 @@ // 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, classify, isBot, login, readActivity, reviewerOf } from "./online-review"; +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 { @@ -35,8 +35,7 @@ const anchorOf = (url: string) => url.slice(url.lastIndexOf("#")); /** * 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 answers. A comment answers each pass it links, - * else, when its first word takes a side as `online-review` reads answers, the latest pass before it with findings. A + * 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. */ @@ -92,17 +91,11 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] .map((pass) => ({ ...pass, titles: bodyFindings(pass.body), answered: false })) .filter((pass) => pass.titles.length > 0) .sort((a, b) => a.at - b.at); - for (const comment of comments) { - if (!comment.writer) continue; - const linked = passes.filter((pass) => comment.at >= pass.at && comment.body.includes(anchorOf(pass.url))); - const targets = - linked.length > 0 - ? linked - : classify(comment.body) === "unclassified" - ? [] - : passes.filter((pass) => pass.at <= comment.at).slice(-1); - for (const pass of targets) pass.answered = true; - } + // 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 && comment.body.includes(anchorOf(pass.url)), + ); for (const pass of passes) if (!pass.answered) for (const title of pass.titles) diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index 4333e285..f1f2d2a2 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -242,8 +242,9 @@ function deps(world: World): GitHubDependencies { if (mutation === "dequeuePullRequest" || mutation === "disablePullRequestAutoMerge") { const pull = pullOf(); world.mutations.push(`${mutation} ${pull.number}`); + // Like GitHub, leaving the queue also disarms auto-merge. if (mutation === "dequeuePullRequest") pull.queued = undefined; - else pull.armed = undefined; + pull.armed = undefined; return Response.json({ data: { [mutation]: { clientMutationId: null } } }); } if (mutation === "enqueuePullRequest" || mutation === "enablePullRequestAutoMerge") { @@ -812,11 +813,8 @@ test("a queued or armed pick that draws an online finding leaves the queue, once `dequeued 30 ${HEAD} 1 signals`, `dequeued 31 ${HEAD} 1 signals`, ]); - expect(github.mutations).toEqual([ - "dequeuePullRequest 30", - "disablePullRequestAutoMerge 30", - "disablePullRequestAutoMerge 31", - ]); + // 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`, @@ -824,7 +822,7 @@ test("a queued or armed pick that draws an online finding leaves the queue, once 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(3); + expect(github.mutations).toHaveLength(2); expect(github.comments[31]).toHaveLength(1); }); diff --git a/test/signals.test.ts b/test/signals.test.ts index 8d9f130f..299bcbf6 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -84,7 +84,7 @@ group("signals", () => { expect(signals(pull, WRITERS)).toHaveLength(1); }); - test("a body finding on the head holds it until a writer's later comment answers that pass", () => { + 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)", "", @@ -96,7 +96,7 @@ group("signals", () => { ["review (OpenAI)", "share handover credit across predecessors"], ["review (OpenAI)", "name the timeout"], ]); - // Before the pass, by a bot or a reader, or taking no side and linking nothing: no answer. + // 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"), @@ -108,12 +108,13 @@ group("signals", () => { 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)).toEqual([]); + expect(signals(pull, WRITERS)).toHaveLength(2); }); - test("an answer clears only the passes it links, else the latest before it", () => { + 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"), @@ -124,8 +125,8 @@ group("signals", () => { ], }); expect(signals(pull, WRITERS).map((signal) => signal.text)).toEqual(["name it"]); - pull.comments = [comment(person, "Declined: checked above.", "2026-11-10T17:07:00Z", "1")]; - expect(signals(pull, WRITERS).map((signal) => signal.text)).toEqual(["guard null"]); + 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", () => { From 7989af8bbcfa23c145afa7f3637b142539619aee Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 00:08:58 -0800 Subject: [PATCH 05/21] fix(signals): say how to answer a signal in each refusal and comment Co-Authored-By: Claude Opus 5.5 --- src/cli/hold.ts | 2 +- src/cli/land-watch.ts | 2 +- src/cli/land.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli/hold.ts b/src/cli/hold.ts index 85b22803..a64a491c 100644 --- a/src/cli/hold.ts +++ b/src/cli/hold.ts @@ -349,7 +349,7 @@ async function unsignaled(github: GitHub, pr: PullRequest, url: string, sha: str 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 reply to each finding and link each decision in a comment that answers it, then land it again`, + `${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`, ); } diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 4e66acfe..42e354e9 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -377,7 +377,7 @@ async function dequeueSignaled(github: GitHub, repository: Repository, pulls: Ob github, { repository: `${repository.owner}/${repository.repo}` }, { number, enabledAt: pick }, - `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, since its head ${head} is held by ${describe(found.signals)}. Push a fix, or answer each, then land it again.`, + `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, 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.`, `signaled ${head}`, "quest land watch", "unanswered_signal", diff --git a/src/cli/land.ts b/src/cli/land.ts index 17a14a22..6a416c09 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -433,7 +433,7 @@ export async function resolveQueue( } 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, then land it again`, + `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. From 9c6af83d25ebbfb4cadd421b0fe99540f963e148 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 00:20:22 -0800 Subject: [PATCH 06/21] fix(signals): answer CodeRabbit's threads on the late-signal hold Recheck signals before a direct merge and in the auto-merge refusal fallback, count a named review only under a bot's or a writer's account, match whole anchors, report OWNER in the fake, and name the next step for an unanswered_signal under a fixer. Co-Authored-By: Claude Opus 5.5 --- assets/skills/run.md | 3 ++- src/cli/hold.ts | 3 +++ src/cli/online-review.ts | 2 +- src/cli/signals.ts | 17 +++++++++++++---- test/github.ts | 3 ++- test/hold.test.ts | 27 +++++++++++++++++++++++++++ test/signals.test.ts | 25 +++++++++++++++++++++++++ 7 files changed, 73 insertions(+), 7 deletions(-) diff --git a/assets/skills/run.md b/assets/skills/run.md index ac3e349f..bcfbe64a 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`, or `unanswered_signal` the pick holds, even from an earlier session: dispatch `/quest-iterate`. After `checks_pending`, run `/quest-merge` again once they pass. +An `unanswered_signal`, which no fixer takes, holds the pick even with one: dispatch `/quest-iterate`. +Without a fixer, after `merge_conflict`, `failed_checks`, or `unanswered_signal` the pick holds, even from an earlier session: dispatch `/quest-iterate`. After `checks_pending`, run `/quest-merge` again once they pass. Any other reason, such as a person removing it, is the user's call. ## Loop diff --git a/src/cli/hold.ts b/src/cli/hold.ts index a64a491c..a05dd03c 100644 --- a/src/cli/hold.ts +++ b/src/cli/hold.ts @@ -290,6 +290,8 @@ export async function land( ); } 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); const submit = (mutation: "mergePullRequest" | "enablePullRequestAutoMerge") => labels.github.graphql( `mutation($id: ID!, $sha: GitObjectID!, $method: PullRequestMergeMethod!) { @@ -318,6 +320,7 @@ export async function land( (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"); } } diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index d982c0cb..5cd85536 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -56,7 +56,7 @@ export function bodyFindings(body: string): string[] { 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 author { __typename login } authorAssociation body submittedAt commit { oid }", reviewThreads: "id isResolved resolvedBy { __typename login } " + `comments(first: 100) { pageInfo { hasNextPage endCursor } nodes { ${THREAD_COMMENT} } }`, diff --git a/src/cli/signals.ts b/src/cli/signals.ts index 90f1f654..d6f5231c 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -32,6 +32,13 @@ const firstLine = (body: string) => /** 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, @@ -45,7 +52,8 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] const reviews = pull.reviews.map((review) => ({ id: String(review.id), url: String(review.url), - reviewer: reviewerOf(review), + // 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)), head: review.commit != null && String(object(review.commit).oid) === pull.head, @@ -94,7 +102,7 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] // 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 && comment.body.includes(anchorOf(pass.url)), + (comment) => comment.writer && comment.at >= pass.at && links(comment.body, pass.url), ); for (const pass of passes) if (!pass.answered) @@ -104,8 +112,8 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] const committed = Date.parse(pull.committed); for (const decision of comments) { if (!decision.writer || !DECISION.test(decision.body) || decision.at <= committed) continue; - const anchor = anchorOf(decision.url); - if (comments.some((reply) => reply.writer && reply.at > decision.at && reply.body.includes(anchor))) continue; + if (comments.some((reply) => reply.writer && reply.at > decision.at && links(reply.body, decision.url))) + continue; found.push({ kind: "decision", url: decision.url, @@ -152,6 +160,7 @@ export async function readSignals( ): Promise> { const pulls = await readActivity(client, repository, numbers); 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) => ({ diff --git a/test/github.ts b/test/github.ts index e6cc70fb..e9b32a4e 100644 --- a/test/github.ts +++ b/test/github.ts @@ -335,6 +335,7 @@ function activityOf(repo: Repo, number: number) { 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 }, @@ -358,7 +359,7 @@ function activityOf(repo: Repo, number: number) { comments: page( (pull.comments ?? []).map((comment, index) => ({ author: actorOf(comment.author), - authorAssociation: "COLLABORATOR", + authorAssociation: comment.author === repo.owner ? "OWNER" : "COLLABORATOR", body: comment.body, createdAt: comment.at, url: `${url}#issuecomment-${index}`, diff --git a/test/hold.test.ts b/test/hold.test.ts index 3d8e0b03..51d750a4 100644 --- a/test/hold.test.ts +++ b/test/hold.test.ts @@ -35,6 +35,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. */ @@ -111,6 +113,7 @@ class FakePull { if (url.pathname === "/repos/example/project/branches/main") return json(200, { protection: {} }); 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") { @@ -379,6 +382,30 @@ describe("merge --land", () => { 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, HEAD, "merge", pull.deps(), true))).toContain("Maintainer decision: late."); + 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/signals.test.ts b/test/signals.test.ts index 299bcbf6..8d55e3d6 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -175,4 +175,29 @@ group("signals", () => { 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("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([]); + }); }); From fb1b38f16cb554fa23681974dc155e5144bd7394 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 02:15:51 -0800 Subject: [PATCH 07/21] fix(land-watch): skip a signaled pick retargeted off the watched base Co-Authored-By: Claude Opus 5.5 --- src/cli/land-watch.ts | 11 ++++++++--- test/land-watch.test.ts | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 42e354e9..3ce9f6f1 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -321,7 +321,12 @@ async function enqueueArmed( * 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, pulls: ObjectValue[]): Promise { +async function dequeueSignaled( + github: GitHub, + repository: Repository, + base: string, + pulls: ObjectValue[], +): Promise { const picked = pulls.filter((pull) => pull.mergeQueueEntry != null || pull.autoMergeRequest != null); if (picked.length === 0) return []; const output: string[] = []; @@ -336,7 +341,7 @@ async function dequeueSignaled(github: GitHub, repository: Repository, pulls: Ob 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.headRefOid) !== head) continue; + if (now.state !== "OPEN" || string(now.baseRefName) !== base || string(now.headRefOid) !== head) continue; const pick = now.autoMergeRequest != null ? string(object(now.autoMergeRequest).enabledAt) @@ -764,7 +769,7 @@ export async function watchPass( save(path, progress); } } - output.push(...(await dequeueSignaled(github, repository, pulls))); + output.push(...(await dequeueSignaled(github, repository, ref, pulls))); 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(); diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index f1f2d2a2..d6d9cb62 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -772,7 +772,7 @@ 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", async () => { +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 = { @@ -798,6 +798,8 @@ test("a queued or armed pick that draws an online finding leaves the queue, once 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, @@ -824,6 +826,7 @@ test("a queued or armed pick that draws an online finding leaves the queue, once 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 refused enqueue of an armed pick is reported once and retried, and says nothing when GitHub queued it first", async () => { From 8ae4bb75205758ca90d7c1f3ca16d1b90c542d49 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 12:40:31 -0800 Subject: [PATCH 08/21] fix(signals): tighten reviewer replies, thread-only passes, fenced decisions, paged head reads, and comment-first dequeue Co-Authored-By: Claude Opus 5.5 --- README.md | 8 ++++---- src/cli/land-watch.ts | 40 +++++++++++++++++++------------------- src/cli/online-review.ts | 5 ++++- src/cli/signals.ts | 22 ++++++++++++++------- test/land-watch.test.ts | 36 ++++++++++++++++++++++++++++++++++ test/online-review.test.ts | 39 ++++++++++++++++++++++++++++++++++++- test/signals.test.ts | 33 +++++++++++++++++++++++++++++-- 7 files changed, 148 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index d2c462d6..79a0bb78 100644 --- a/README.md +++ b/README.md @@ -872,11 +872,11 @@ 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 replies. A `[P]` line in its latest - review body over the head holds until a writer's later comment links that - review. + 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, posted after the head's commit, which therefore cannot apply it. A + a quote or code, 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. A writer can push to the repository: an owner, or a member or collaborator diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 3ce9f6f1..d1e88280 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -349,26 +349,36 @@ async function dequeueSignaled( ? string(object(now.mergeQueueEntry).enqueuedAt) : null; if (pick === null) continue; - // 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 moved first and neither was taken, the next pass reads it again. - let taken = false; + // The comment goes first: once the PR leaves the queue no pass reads it again, so a comment that failed after + // the dequeue would never be retried. It posts once per pick and head, so a pass that fails below retries only + // the mutations. + await post( + github, + { repository: `${repository.owner}/${repository.repo}` }, + { number, enabledAt: pick }, + `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, 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.`, + `signaled ${head}`, + "quest land watch", + "unanswered_signal", + ); + // GitHub may disarm auto-merge with the dequeue, so the disarm reads the PR again first. try { - if (now.mergeQueueEntry != null) { + if (now.mergeQueueEntry != null) await github.graphql( "mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", { id: pull.id }, ); - taken = true; - } - const armed = taken ? (await readPull(github, string(pull.id))).autoMergeRequest : now.autoMergeRequest; - if (armed != null) { + const armed = + now.mergeQueueEntry != null + ? (await readPull(github, string(pull.id))).autoMergeRequest + : now.autoMergeRequest; + if (armed != null) await github.graphql( "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", { id: pull.id }, ); - taken = true; - } } catch (error) { + // GitHub moving first, such as dequeuing it itself, leaves nothing to take. if (!(error instanceof GitHubGraphQLError)) throw error; const fresh = await readPull(github, string(pull.id)); const ended = @@ -376,17 +386,7 @@ async function dequeueSignaled( string(fresh.headRefOid) !== head || (fresh.mergeQueueEntry == null && fresh.autoMergeRequest == null); if (!ended) throw error; - if (!taken) continue; } - await post( - github, - { repository: `${repository.owner}/${repository.repo}` }, - { number, enabledAt: pick }, - `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, 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.`, - `signaled ${head}`, - "quest land watch", - "unanswered_signal", - ); output.push(`dequeued ${number} ${head} ${found.signals.length} signals`); } return output; diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index 5cd85536..9987bc0b 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -117,12 +117,15 @@ export async function readActivity( 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) + throw new Error(`the head of PR #${number} changed while it was read; retry`); const next: Partial> = {}; for (const name of names) { const connection = object(pull[name]); diff --git a/src/cli/signals.ts b/src/cli/signals.ts index d6f5231c..cb30d7ab 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -14,8 +14,8 @@ export interface Signal { text: string; } -/** The marker a comment opens with to record a maintainer decision, after markup such as `**` but not in a quote. */ -export const DECISION = /^[^A-Za-z>]*Maintainer decision\b/i; +/** The marker a comment opens with to record a maintainer decision, after markup such as `**` but not in a quote or code. */ +export const DECISION = /^[^A-Za-z>`~]*Maintainer decision\b/i; /** Repository permissions that can push, whose decisions and answers count. */ const WRITE = ["admin", "maintain", "write"]; @@ -59,6 +59,12 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] head: review.commit != null && String(object(review.commit).oid) === pull.head, })); 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, @@ -74,11 +80,13 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] 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 own reviews tell its replies from an answer. + // 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") && - comment.pullRequestReview != null && - byId.get(String(object(comment.pullRequestReview).id))?.reviewer === opener.reviewer; + (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", @@ -88,10 +96,10 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] }); } - // Each reviewer's latest pass over the head lists what it still finds there. + // Each reviewer's latest pass over the head lists what it still finds there; a pass may only open threads. const latest = new Map(); for (const review of reviews) - if (review.reviewer !== null && review.head && review.body.trim() !== "") { + if (review.reviewer !== null && review.head && (review.body.trim() !== "" || opened.has(review.id))) { const prior = latest.get(review.reviewer); if (!prior || prior.at <= review.at) latest.set(review.reviewer, review); } diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index d6d9cb62..e878abc1 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -829,6 +829,42 @@ test("a queued or armed pick that draws an online finding leaves the queue, once expect(github.comments[33]).toBeUndefined(); }); +test("a signaled pick whose comment GitHub refuses stays queued until a pass can say why it leaves", async () => { + const state = tempDir(); + const github = 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 }, + }, + ], + }, + }, + }, + ]); + github.now = Date.parse(at(4)); + github.refuse = ["#30 quest land watch: ended"]; + await expect(watchPass(REPOSITORY, { state }, deps(github))).rejects.toThrow(); + expect(github.mutations).toEqual([]); + expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([`dequeued 30 ${HEAD} 1 signals`]); + expect(github.mutations).toEqual(["dequeuePullRequest 30"]); + expect(github.comments[30]).toHaveLength(1); +}); + 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([ diff --git a/test/online-review.test.ts b/test/online-review.test.ts index 9e288f04..97da78f5 100644 --- a/test/online-review.test.ts +++ b/test/online-review.test.ts @@ -1,6 +1,7 @@ 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"; const HEAD = "h".repeat(40); const bot = (login: string) => ({ __typename: "Bot", login }); @@ -157,3 +158,39 @@ describe("onlineReviews", () => { ]); }); }); + +test("a head pushed while later pages are read fails the read", async () => { + const page = (nodes: object[], next: boolean) => ({ + pageInfo: { hasNextPage: next, endCursor: next ? "1" : null }, + nodes, + }); + const read = (moved: boolean) => { + const client = { + 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; + return readActivity(client, { owner: "acme", repo: "proj" } as Repository, [1]); + }; + expect((await read(false)).get(1)!.reviews).toHaveLength(2); + await expect(read(true)).rejects.toThrow("head of PR #1 changed while it was read"); +}); diff --git a/test/signals.test.ts b/test/signals.test.ts index 8d55e3d6..ca0ec053 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -79,9 +79,17 @@ group("signals", () => { 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")])], + 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"), + ]), + ], }); - expect(signals(pull, WRITERS)).toHaveLength(1); + 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", () => { @@ -140,6 +148,25 @@ group("signals", () => { 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, @@ -156,6 +183,8 @@ group("signals", () => { 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, "Per the Maintainer decision, ...", "2026-11-10T17:13:00Z", "5"), ], }); From d813a06a8ec6dc3cdb904ada94fb0e32e21dec43 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 12:55:37 -0800 Subject: [PATCH 09/21] fix(signals): dequeue before commenting and keep a refused comment, scope the paged head check to signals, skip indented decisions Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/cli/land-watch.ts | 75 +++++++++++++++++++++++++++----------- src/cli/online-review.ts | 10 +++-- src/cli/signals.ts | 9 ++++- test/land-watch.test.ts | 16 ++++++-- test/online-review.test.ts | 22 +++++++---- test/signals.test.ts | 4 +- 7 files changed, 96 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 79a0bb78..e819b46e 100644 --- a/README.md +++ b/README.md @@ -876,7 +876,7 @@ merge ends the pick. There are two kinds: 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, posted after the head's commit, which therefore cannot apply it. A + 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. A writer can push to the repository: an owner, or a member or collaborator diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index d1e88280..005f4dbd 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -137,6 +137,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. */ @@ -326,9 +328,10 @@ async function dequeueSignaled( repository: Repository, base: string, pulls: ObjectValue[], + progress: Progress, + saveProgress: () => void, ): Promise { const picked = pulls.filter((pull) => pull.mergeQueueEntry != null || pull.autoMergeRequest != null); - if (picked.length === 0) return []; const output: string[] = []; const read = await readSignals( github, @@ -349,36 +352,26 @@ async function dequeueSignaled( ? string(object(now.mergeQueueEntry).enqueuedAt) : null; if (pick === null) continue; - // The comment goes first: once the PR leaves the queue no pass reads it again, so a comment that failed after - // the dequeue would never be retried. It posts once per pick and head, so a pass that fails below retries only - // the mutations. - await post( - github, - { repository: `${repository.owner}/${repository.repo}` }, - { number, enabledAt: pick }, - `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, 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.`, - `signaled ${head}`, - "quest land watch", - "unanswered_signal", - ); - // GitHub may disarm auto-merge with the dequeue, so the disarm reads the PR again first. + // 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) + if (now.mergeQueueEntry != null) { await github.graphql( "mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", { id: pull.id }, ); - const armed = - now.mergeQueueEntry != null - ? (await readPull(github, string(pull.id))).autoMergeRequest - : now.autoMergeRequest; - if (armed != null) + taken = true; + } + const armed = taken ? (await readPull(github, string(pull.id))).autoMergeRequest : now.autoMergeRequest; + if (armed != null) { await github.graphql( "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", { id: pull.id }, ); + taken = true; + } } catch (error) { - // GitHub moving first, such as dequeuing it itself, leaves nothing to take. if (!(error instanceof GitHubGraphQLError)) throw error; const fresh = await readPull(github, string(pull.id)); const ended = @@ -386,8 +379,46 @@ async function dequeueSignaled( string(fresh.headRefOid) !== head || (fresh.mergeQueueEntry == null && fresh.autoMergeRequest == null); if (!ended) throw error; + if (!taken) continue; } output.push(`dequeued ${number} ${head} ${found.signals.length} signals`); + // Out of the queue, no later pass reads the PR, so its comment is kept until GitHub takes it. + (progress.unsaid ??= {})[number] = { + pick, + head, + text: `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, 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.`, + }; + saveProgress(); + } + output.push(...(await sayUnsaid(github, repository, progress, saveProgress))); + return output; +} + +/** Post each signal comment a pass could not, leaving those GitHub refuses again for the next pass. */ +async function sayUnsaid( + github: GitHub, + repository: Repository, + progress: Progress, + saveProgress: () => void, +): Promise { + const output: string[] = []; + for (const [number, { pick, head, text }] of Object.entries(progress.unsaid ?? {})) { + try { + await post( + github, + { repository: `${repository.owner}/${repository.repo}` }, + { number: Number(number), enabledAt: pick }, + text, + `signaled ${head}`, + "quest land watch", + "unanswered_signal", + ); + } catch (error) { + output.push(`unsaid ${number} ${head}: ${error instanceof Error ? error.message : String(error)}`); + continue; + } + delete progress.unsaid![Number(number)]; + saveProgress(); } return output; } @@ -769,7 +800,7 @@ export async function watchPass( save(path, progress); } } - output.push(...(await dequeueSignaled(github, repository, ref, pulls))); + output.push(...(await dequeueSignaled(github, repository, ref, pulls, progress, () => save(path, progress)))); 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(); diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index 9987bc0b..3841094e 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -70,8 +70,11 @@ 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 committer date, and its reviews, review threads, and comments as GitHub's GraphQL API gives them. */ -export type Activity = Record & { head: string; committed: 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. */ export async function readActivity( @@ -124,8 +127,7 @@ export async function readActivity( ); 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) - throw new Error(`the head of PR #${number} changed while it was read; retry`); + if (pull.headRefOid !== nodes.head) nodes.moved = true; const next: Partial> = {}; for (const name of names) { const connection = object(pull[name]); diff --git a/src/cli/signals.ts b/src/cli/signals.ts index cb30d7ab..2cfbe5ca 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -14,8 +14,11 @@ export interface Signal { text: string; } -/** The marker a comment opens with to record a maintainer decision, after markup such as `**` but not in a quote or code. */ -export const DECISION = /^[^A-Za-z>`~]*Maintainer decision\b/i; +/** + * 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"]; @@ -167,6 +170,8 @@ export async function readSignals( numbers: number[], ): 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 })), diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index e878abc1..88ddcc38 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -829,7 +829,7 @@ test("a queued or armed pick that draws an online finding leaves the queue, once expect(github.comments[33]).toBeUndefined(); }); -test("a signaled pick whose comment GitHub refuses stays queued until a pass can say why it leaves", async () => { +test("a signaled pick leaves the queue even when GitHub refuses its comment, which a later pass posts", async () => { const state = tempDir(); const github = world([ { @@ -858,11 +858,19 @@ test("a signaled pick whose comment GitHub refuses stays queued until a pass can ]); github.now = Date.parse(at(4)); github.refuse = ["#30 quest land watch: ended"]; - await expect(watchPass(REPOSITORY, { state }, deps(github))).rejects.toThrow(); - expect(github.mutations).toEqual([]); - expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([`dequeued 30 ${HEAD} 1 signals`]); + 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"]); }); test("a refused enqueue of an armed pick is reported once and retried, and says nothing when GitHub queued it first", async () => { diff --git a/test/online-review.test.ts b/test/online-review.test.ts index 97da78f5..5e5290f4 100644 --- a/test/online-review.test.ts +++ b/test/online-review.test.ts @@ -2,6 +2,7 @@ import { describe, expect, test } from "vitest"; 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 }); @@ -159,13 +160,13 @@ describe("onlineReviews", () => { }); }); -test("a head pushed while later pages are read fails the read", async () => { +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 read = (moved: boolean) => { - const client = { + const client = (moved: boolean) => + ({ graphql: async (query: string) => { if (query.includes("$reviews")) return { @@ -188,9 +189,14 @@ test("a head pushed while later pages are read fails the read", async () => { }, }; }, - } as unknown as GitHub; - return readActivity(client, { owner: "acme", repo: "proj" } as Repository, [1]); - }; - expect((await read(false)).get(1)!.reviews).toHaveLength(2); - await expect(read(true)).rejects.toThrow("head of PR #1 changed while it was read"); + }) 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", + ]); }); diff --git a/test/signals.test.ts b/test/signals.test.ts index ca0ec053..8bd3cf90 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -170,7 +170,7 @@ group("signals", () => { 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.", + " **Maintainer decision** (2026-11-10): raise the timeout.\n\nDetails.", "2026-11-10T17:12:00Z", "9", ); @@ -185,6 +185,8 @@ group("signals", () => { 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"), ], }); From 33e9cb9fecf218de4909c78077e5af325ee248e8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 13:10:48 -0800 Subject: [PATCH 10/21] fix(signals): mark long-thread pages from a moved head, keep the comment from before the dequeue, and bound its retries Co-Authored-By: Claude Opus 5.5 --- src/cli/land-watch.ts | 73 ++++++++++++++++++++++++++++--------- src/cli/online-review.ts | 2 + test/land-watch.test.ts | 75 ++++++++++++++++++++++++++------------ test/online-review.test.ts | 42 +++++++++++++++++++++ 4 files changed, 150 insertions(+), 42 deletions(-) diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 005f4dbd..9e09302a 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -17,7 +17,7 @@ import { 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, fixerText, post } from "./land-fixer"; import { apiDependencies, daemonLoop, @@ -138,7 +138,7 @@ interface Progress { /** 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; + unsaid?: Record; } /** How far back of the last pass each pass reads again, for an event GitHub shows late. */ @@ -352,6 +352,16 @@ async function dequeueSignaled( ? 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, taking it out of the queue and disarming its auto-merge, 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; @@ -379,22 +389,24 @@ async function dequeueSignaled( string(fresh.headRefOid) !== head || (fresh.mergeQueueEntry == null && fresh.autoMergeRequest == null); if (!ended) throw error; - if (!taken) continue; + // 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(`dequeued ${number} ${head} ${found.signals.length} signals`); - // Out of the queue, no later pass reads the PR, so its comment is kept until GitHub takes it. - (progress.unsaid ??= {})[number] = { - pick, - head, - text: `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, 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.`, - }; - saveProgress(); } output.push(...(await sayUnsaid(github, repository, progress, saveProgress))); return output; } -/** Post each signal comment a pass could not, leaving those GitHub refuses again for the next pass. */ +/** + * 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, @@ -402,23 +414,48 @@ async function sayUnsaid( saveProgress: () => void, ): Promise { const output: string[] = []; - for (const [number, { pick, head, text }] of Object.entries(progress.unsaid ?? {})) { + 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: Number(number), enabledAt: pick }, - text, - `signaled ${head}`, + { number, enabledAt: entry.pick }, + entry.text, + `signaled ${entry.head}`, "quest land watch", "unanswered_signal", ); } catch (error) { - output.push(`unsaid ${number} ${head}: ${error instanceof Error ? error.message : String(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; } - delete progress.unsaid![Number(number)]; - saveProgress(); + drop(); } return output; } diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index 3841094e..12010e43 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -146,9 +146,11 @@ export async function readActivity( 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); diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index 88ddcc38..b38605c8 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -830,32 +830,34 @@ test("a queued or armed pick that draws an online finding leaves the queue, once }); test("a signaled pick leaves the queue even when GitHub refuses its comment, which a later pass posts", async () => { - const state = tempDir(); - const github = 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 }, - }, - ], + 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)); @@ -871,6 +873,31 @@ test("a signaled pick leaves the queue even when GitHub refuses its comment, whi 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 () => { diff --git a/test/online-review.test.ts b/test/online-review.test.ts index 5e5290f4..b665a9d5 100644 --- a/test/online-review.test.ts +++ b/test/online-review.test.ts @@ -200,3 +200,45 @@ test("a head pushed while later pages are read marks the read moved, which fails "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); +}); From 3d5851163148aaffd236146e54d70c151250439d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 13:18:11 -0800 Subject: [PATCH 11/21] fix(land): count a replaced head's findings and decisions since its pick before re-enqueueing a fix Co-Authored-By: Claude Opus 5.5 --- README.md | 5 +++++ src/cli/land-fixer.ts | 17 ++++++++++++++++ src/cli/land-watch-fix.ts | 21 +++++++++++++------- src/cli/main.ts | 2 +- src/cli/signals.ts | 25 ++++++++++++++++++----- test/land-watch.test.ts | 42 +++++++++++++++++++++++++++++++++++++++ test/land.test.ts | 16 +++++++++++++++ test/signals.test.ts | 17 ++++++++++++++++ 8 files changed, 132 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e819b46e..75fef957 100644 --- a/README.md +++ b/README.md @@ -879,6 +879,11 @@ merge ends the pick. There are two kinds: 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: before +re-enqueueing a fixed head, the fixer counts the findings on the head it +replaced and the decisions posted since that head's pick, and hands the PR back +while any holds. + 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. diff --git a/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index 0f1e02e4..39e81d17 100644 --- a/src/cli/land-fixer.ts +++ b/src/cli/land-fixer.ts @@ -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 { describe, readSignals } from "./signals"; /** * Why a fixer's result cannot be published, which hands its PR back. A push GitHub refused, or a fix @@ -1119,6 +1120,22 @@ async function collectJob( attribution: author(result), }); + // The fix read no finding or decision on the pick it replaces, so those still hold it; the queue + // reads only the new head's. + if (head !== entry.head) { + const [owner, repo] = job.plan.repository.split("/"); + const held = ( + await readSignals( + github, + { owner: owner!, repo: repo! }, + [number], + new Map([[number, { head: entry.head, picked: entry.enabledAt }]]), + ) + ).get(number)!; + if (held.head !== head) throw new FixerRefusal("head moved before it was re-enqueued"); + if (held.signals.length > 0) + throw new FixerRefusal(`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. diff --git a/src/cli/land-watch-fix.ts b/src/cli/land-watch-fix.ts index a234ff30..f4643da6 100644 --- a/src/cli/land-watch-fix.ts +++ b/src/cli/land-watch-fix.ts @@ -6,7 +6,7 @@ import { isHoldLabel } from "../core"; import { array, GitHub, type GitHubDependencies, GitHubGraphQLError, object, type ObjectValue, string } from "./github"; import { type Queued, queueHead } from "../github/merge"; import { UNREFUSED } from "./hold"; -import { describe, readSignals } from "./signals"; +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"; @@ -660,7 +660,10 @@ 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, { + head: entry.head, + picked: entry.enabledAt, + }); save(path, job); } catch (failure) { if (failure instanceof Held) return [...said, ...(await leave())]; @@ -707,7 +710,8 @@ 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. + * refusal, or a PR retargeted from `base`, hands the PR back; a hold leaves it to the session that holds it, and a + * signal on `head` or on the `carried` pick it replaces hands it back too. */ async function requeue( github: GitHub, @@ -716,6 +720,7 @@ 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); @@ -723,11 +728,13 @@ async function requeue( if (string(pull.headRefOid) !== head) throw new FixerRefusal("head moved before it was re-enqueued"); if (!queued(pull, dropped)) { if (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. + // A finding or decision that arrived while the fixer ran holds the head as it would a writer's pick. 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 held = (await readSignals(github, { owner: owner!, repo: repo! }, [Number(pull.number)])).get( - Number(pull.number), - )!; + 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) throw new FixerRefusal(`its head is held by ${describe(held.signals)}`); try { diff --git a/src/cli/main.ts b/src/cli/main.ts index 58cb35e7..d7976470 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -746,7 +746,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. 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. " + + "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/signals.ts b/src/cli/signals.ts index 2cfbe5ca..8d89cdc3 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -14,6 +14,12 @@ export interface Signal { text: string; } +/** A picked head whose signals a later commit carries, and when it was picked. */ +export interface Carried { + head: 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. @@ -48,10 +54,13 @@ const links = (body: string, url: string) => * 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` is a head a writer picked that a commit since carries forward unread, such as the land fixer's: findings on + * it hold too, and so does a decision posted after its pick. */ -export function signals(pull: Activity, writers: ReadonlySet): Signal[] { +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 ? [carried.head] : [])]); const reviews = pull.reviews.map((review) => ({ id: String(review.id), url: String(review.url), @@ -59,7 +68,7 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] reviewer: isBot(review.author) || writer(review.author) ? reviewerOf(review) : null, body: typeof review.body === "string" ? review.body : "", at: Date.parse(String(review.submittedAt)), - head: review.commit != null && String(object(review.commit).oid) === pull.head, + head: review.commit != null && heads.has(String(object(review.commit).oid)), })); const byId = new Map(reviews.map((review) => [review.id, review])); const opened = new Set( @@ -120,7 +129,7 @@ export function signals(pull: Activity, writers: ReadonlySet): Signal[] for (const title of pass.titles) found.push({ kind: "finding", url: pass.url, author: pass.reviewer!, text: title }); - const committed = Date.parse(pull.committed); + 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))) @@ -163,11 +172,12 @@ async function writersOf( return writers; } -/** The signals on each PR's current head, by number, with the head they were read for. */ +/** 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) @@ -183,7 +193,12 @@ export async function readSignals( ), ]); const writers = await writersOf(client, repository, actors); - return new Map([...pulls].map(([number, pull]) => [number, { head: pull.head, signals: signals(pull, writers) }])); + 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. */ diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index b38605c8..c9504943 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -1338,6 +1338,48 @@ 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"); + }); + 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 e3d71c08..5e055049 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -2987,6 +2987,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; diff --git a/test/signals.test.ts b/test/signals.test.ts index 8bd3cf90..2e1d3cef 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -217,6 +217,23 @@ group("signals", () => { 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 = { head: 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("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")], From 4bd5446323d538c864d5cf6e8f2a1803df9d781d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 14:22:57 -0800 Subject: [PATCH 12/21] fix(land): carry a replaced pick's signals to the fixer's head in every queue check Co-Authored-By: Claude Opus 5.5 --- README.md | 10 +++++---- src/cli/land-fixer.ts | 34 ++++++++++++++++++++++++++----- src/cli/land-head.ts | 6 +++++- src/cli/land-watch-fix.ts | 14 +++++++++---- src/cli/land-watch.ts | 8 ++++++-- src/cli/land.ts | 14 +++++++++++-- src/cli/signals.ts | 10 ++++----- test/land-watch.test.ts | 43 +++++++++++++++++++++++++++++++++++++++ test/land.test.ts | 26 +++++++++++++++++++++++ test/signals.test.ts | 2 +- 10 files changed, 143 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 75fef957..bf495335 100644 --- a/README.md +++ b/README.md @@ -879,10 +879,12 @@ merge ends the pick. There are two kinds: 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: before -re-enqueueing a fixed head, the fixer counts the findings on the head it -replaced and the decisions posted since that head's pick, and hands the PR back -while any holds. +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. A writer's push or a handback ends +the carry. 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. diff --git a/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index 39e81d17..4061f9fb 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,7 +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 { describe, readSignals } from "./signals"; +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 @@ -207,6 +207,30 @@ 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 ?? [] }; } +/** + * What the fixer's `head` of PR `number` carries from the pick it replaces: the heads its rounds replaced up to the + * round that put `head` back in the queue, and when the pick was made. A head no round put back, or a pick a handback + * ended, carries nothing, so only the fixer's own commits inherit, however their head is queued. + */ +export function carriedBy(state: string, number: number, head: string): Carried | undefined { + const record = history(state, number); + if (!record || record.rounds.at(-1)?.outcome === "handback" || !Number.isFinite(Date.parse(record.pick))) + return undefined; + let end = record.rounds.length; + while (end > 0 && record.rounds[end - 1]!.after !== head) end--; + if (end === 0) return undefined; + return { heads: record.rounds.slice(0, end).map((round) => round.before), picked: record.pick }; +} +/** 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] }; @@ -1120,16 +1144,16 @@ async function collectJob( attribution: author(result), }); - // The fix read no finding or decision on the pick it replaces, so those still hold it; the queue - // reads only the new head's. + // 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], - new Map([[number, { head: entry.head, picked: entry.enabledAt }]]), + carried && new Map([[number, carried]]), ) ).get(number)!; if (held.head !== head) throw new FixerRefusal("head moved before it was re-enqueued"); diff --git a/src/cli/land-head.ts b/src/cli/land-head.ts index 51862ca4..969c2979 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 { carriedOf } 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: (pulls) => carriedOf(state, pulls), + }); 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 f4643da6..6298d8ce 100644 --- a/src/cli/land-watch-fix.ts +++ b/src/cli/land-watch-fix.ts @@ -22,6 +22,7 @@ import { fixerText, history, type History, + carriedBy, historyPath, identity, importFix, @@ -660,10 +661,15 @@ async function collect( job.requeuing = head; save(path, job); } - job.requeued = await requeue(github, entry.id, head, job.base.ref, job.dropped, queue, { - head: entry.head, - picked: entry.enabledAt, - }); + 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())]; diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 9e09302a..8acf6f5b 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -17,7 +17,7 @@ import { import type { Entry, LandMethod, Plan } from "./land"; import { authenticatedRead } from "./land-git"; import { jobLog } from "./land-checks"; -import { COLLECTION_ATTEMPTS, fixerText, post } from "./land-fixer"; +import { COLLECTION_ATTEMPTS, carriedOf, fixerText, post } from "./land-fixer"; import { apiDependencies, daemonLoop, @@ -330,6 +330,7 @@ async function dequeueSignaled( pulls: ObjectValue[], progress: Progress, saveProgress: () => void, + state: string, ): Promise { const picked = pulls.filter((pull) => pull.mergeQueueEntry != null || pull.autoMergeRequest != null); const output: string[] = []; @@ -337,6 +338,7 @@ async function dequeueSignaled( github, repository, picked.map((pull) => Number(pull.number)), + carriedOf(state, picked), ); for (const pull of picked) { const number = Number(pull.number); @@ -837,7 +839,9 @@ export async function watchPass( save(path, progress); } } - output.push(...(await dequeueSignaled(github, repository, ref, pulls, progress, () => 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(); diff --git a/src/cli/land.ts b/src/cli/land.ts index 6a416c09..6e267f64 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -18,7 +18,7 @@ import { } from "./github"; import { appOf, latestContexts } from "../github/checks"; import { classifyCheck } from "./status"; -import { describe, readSignals } from "./signals"; +import { type Carried, describe, readSignals } from "./signals"; import type { Method } from "./hold"; import { committedSources, snapshot } from "./tree"; @@ -302,6 +302,14 @@ async function unresolved(github: GitHub, id: string): Promise { } } +/** 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 each landing PR's head carries from a pick the land fixer replaced, by number. */ + carried?: (pulls: ObjectValue[]) => ReadonlyMap; +} + /** * 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 @@ -312,8 +320,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()); @@ -394,6 +403,7 @@ export async function resolveQueue( github, repository, landing.map((node) => Number(node.number)), + carried?.(landing), ); for (const node of landing) { const request = object(node.autoMergeRequest); diff --git a/src/cli/signals.ts b/src/cli/signals.ts index 8d89cdc3..09ed5c0c 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -14,9 +14,9 @@ export interface Signal { text: string; } -/** A picked head whose signals a later commit carries, and when it was picked. */ +/** The picked heads whose signals a later commit carries unread, and when the pick was made. */ export interface Carried { - head: string; + heads: string[]; picked: string; } @@ -54,13 +54,13 @@ const links = (body: string, url: string) => * 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` is a head a writer picked that a commit since carries forward unread, such as the land fixer's: findings on - * it hold too, and so does a decision posted after its pick. + * `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 ? [carried.head] : [])]); + const heads = new Set([pull.head, ...(carried?.heads ?? [])]); const reviews = pull.reviews.map((review) => ({ id: String(review.id), url: String(review.url), diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index c9504943..fa0b5ac4 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -1380,6 +1380,49 @@ describe.skipIf(process.platform === "win32")("the watcher's land fixer", () => expect(github.comments[5].at(-1)!.body).toContain("finding by rabbit[bot]: drops the last frame"); }); + 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 5e055049..36feb455 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -27,12 +27,14 @@ import { ADMITTED, author, COLLECTION_ATTEMPTS, + carriedBy, collectFixers, validateFix, fixerMarkdown, fixerText, REQUEUE_LIMIT, history, + historyPath, redactTokens, type Round, stopReason, @@ -4370,6 +4372,30 @@ describe("land fixer stop rules", () => { }); }); +test("a fixed head carries the heads its pick's rounds replaced, until a handback ends the pick", () => { + const state = tempDir(); + const save = (record: object) => writeFileSync(historyPath(state, 5), JSON.stringify(record)); + const round = (before: string, after?: string): Round => ({ + job: before, + outcome: after ? "fixed" : "handback", + before, + failure: "f", + ...(after ? { after } : {}), + }); + const pick = "2026-11-10T17:00:00Z"; + expect(carriedBy(state, 5, "b")).toBeUndefined(); + save({ pick, rounds: [round("a", "b"), round("b", "c")] }); + expect(carriedBy(state, 5, "c")).toEqual({ heads: ["a", "b"], picked: pick }); + expect(carriedBy(state, 5, "b")).toEqual({ heads: ["a"], picked: pick }); + // A head no round put back, such as a writer's push, carries nothing. + expect(carriedBy(state, 5, "d")).toBeUndefined(); + save({ pick, rounds: [round("a", "b"), round("b")] }); + expect(carriedBy(state, 5, "b")).toBeUndefined(); + // A pick that is not a time cannot bound decisions. + save({ pick: "f".repeat(40), rounds: [round("a", "b")] }); + expect(carriedBy(state, 5, "b")).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)}`], { diff --git a/test/signals.test.ts b/test/signals.test.ts index 2e1d3cef..81d579d4 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -227,7 +227,7 @@ group("signals", () => { ], }); expect(signals(pull, WRITERS)).toEqual([]); - const carried = { head: OLD, picked: "2026-11-10T17:00:00Z" }; + 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."], From 0aed1254e514a295dc9e51eb9a2ded6c9c28962f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 14:59:01 -0800 Subject: [PATCH 13/21] fix(land): carry a prepared head's pin, disarm only the pick read, and word an armed-only dequeue Co-Authored-By: Claude Opus 5.5 --- src/cli/land-head.ts | 4 ++-- src/cli/land-watch.ts | 12 +++++++++--- src/cli/land.ts | 24 +++++++++++++++++++++--- test/land.test.ts | 17 +++++++++++++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/cli/land-head.ts b/src/cli/land-head.ts index 969c2979..d462130b 100644 --- a/src/cli/land-head.ts +++ b/src/cli/land-head.ts @@ -1,7 +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 { carriedOf } from "./land-fixer"; +import { carriedBy } from "./land-fixer"; import { gitAuthentication } from "./land-git"; import { GitHub, type GitHubDependencies, type Repository, object, string } from "./github"; import { @@ -51,7 +51,7 @@ export async function resolvePreparedQueue( ): Promise { const plan = await resolveQueue(repository, target, deps, { acceptHead: (entry) => isPrepared(root, state, entry), - carried: (pulls) => carriedOf(state, pulls), + carried: (number, head) => carriedBy(state, number, head), }); const numbers = new Set( readdirSync(state).flatMap((name) => { diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index 8acf6f5b..e4dcee2e 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -360,7 +360,7 @@ async function dequeueSignaled( id: string(pull.id), pick, head, - text: `quest land watch: ended this pull request's pick, taking it out of the queue and disarming its auto-merge, 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.`, + text: `quest land watch: ended this pull request's pick, ${now.mergeQueueEntry != null ? "taking it out of the queue and disarming its auto-merge" : "disarming its auto-merge"}, 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(); @@ -375,8 +375,14 @@ async function dequeueSignaled( ); taken = true; } - const armed = taken ? (await readPull(github, string(pull.id))).autoMergeRequest : now.autoMergeRequest; - if (armed != null) { + // 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 }, diff --git a/src/cli/land.ts b/src/cli/land.ts index 6e267f64..dd02a347 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -302,12 +302,25 @@ 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 each landing PR's head carries from a pick the land fixer replaced, by number. */ - carried?: (pulls: ObjectValue[]) => ReadonlyMap; + /** What PR `number`'s `head` carries from a pick the land fixer replaced. */ + carried?: (number: number, head: string) => Carried | undefined; } /** @@ -403,7 +416,12 @@ export async function resolveQueue( github, repository, landing.map((node) => Number(node.number)), - carried?.(landing), + 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); diff --git a/test/land.test.ts b/test/land.test.ts index 36feb455..5eab23a4 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -814,6 +814,23 @@ 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, 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" }); From bb172ff457ecfc48e9ea02013b221e3eb9bddc5a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 15:09:06 -0800 Subject: [PATCH 14/21] test(land): pass deps to prepareHead after merging main Co-Authored-By: Claude Opus 5.5 --- test/land.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/land.test.ts b/test/land.test.ts index 90727f81..daf5a364 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -872,7 +872,16 @@ describe.skipIf(process.platform === "win32")("quest land", () => { 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, async () => deps.token); + 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 }, ]; From 9ed725a62317cb70a0c5cdbcd8b2f75aba6c4885 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 15:36:55 -0800 Subject: [PATCH 15/21] fix(land): check a queued fixer head, carry gate pins and past handbacks, and keep each head's passes apart Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- src/cli/land-fixer.ts | 27 ++++++++++++++++++++------- src/cli/land-watch-fix.ts | 31 +++++++++++++++++++++---------- src/cli/land-watch.ts | 4 +++- src/cli/online-review.ts | 2 +- src/cli/signals.ts | 20 +++++++++++++++----- test/land-watch.test.ts | 2 +- test/land.test.ts | 7 ++++--- test/signals.test.ts | 12 ++++++++++++ 9 files changed, 79 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c69fed70..458609fd 100644 --- a/README.md +++ b/README.md @@ -890,8 +890,8 @@ 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. A writer's push or a handback ends -the carry. +serve`'s queue), however the head got queued and even after a handback. Only a writer's +push ends the carry. 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. diff --git a/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index 9bf788f4..faf5dcf1 100644 --- a/src/cli/land-fixer.ts +++ b/src/cli/land-fixer.ts @@ -176,6 +176,8 @@ export interface Round { outcome: Outcome; /** The head that failed. */ before: string; + /** 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 PR went back to the queue at, which a fix or merge moves; a handback has none. */ after?: string; /** The failure's signature: what failed, so a recurrence after a fix can be told from a new failure. */ @@ -209,17 +211,20 @@ export function history(state: string, number: number): History | undefined { } /** * What the fixer's `head` of PR `number` carries from the pick it replaces: the heads its rounds replaced up to the - * round that put `head` back in the queue, and when the pick was made. A head no round put back, or a pick a handback - * ended, carries nothing, so only the fixer's own commits inherit, however their head is queued. + * round that put `head` back in the queue, with the pins the gate's base merges stood on, and when the pick was made. + * A handback leaves the carry, 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 record = history(state, number); - if (!record || record.rounds.at(-1)?.outcome === "handback" || !Number.isFinite(Date.parse(record.pick))) - return undefined; + if (!record || !Number.isFinite(Date.parse(record.pick))) return undefined; let end = record.rounds.length; while (end > 0 && record.rounds[end - 1]!.after !== head) end--; if (end === 0) return undefined; - return { heads: record.rounds.slice(0, end).map((round) => round.before), picked: record.pick }; + return { + heads: record.rounds.slice(0, end).flatMap((round) => (round.pin ? [round.before, round.pin] : [round.before])), + picked: record.pick, + }; } /** What each of `pulls`' heads carries from a pick the land fixer replaced, by number. */ export function carriedOf(state: string, pulls: ObjectValue[]): Map { @@ -1108,9 +1113,17 @@ async function collectJob( { outcome, failure }, ); if (rule) error = stopReason(rule, { outcome, failure }); + const pin = standing !== entry.head ? { pin: entry.head } : {}; 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) { diff --git a/src/cli/land-watch-fix.ts b/src/cli/land-watch-fix.ts index 2b7d28bb..7c355808 100644 --- a/src/cli/land-watch-fix.ts +++ b/src/cli/land-watch-fix.ts @@ -732,17 +732,28 @@ async function requeue( 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, + }); + if (queued(pull, dropped) && pull.autoMergeRequest != null) + await github.graphql( + "mutation($id: ID!) { disablePullRequestAutoMerge(input: { pullRequestId: $id }) { clientMutationId } }", + { id }, + ); + throw new FixerRefusal(`its head is held by ${describe(held.signals)}`); + } if (!queued(pull, dropped)) { - if (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. 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) throw new FixerRefusal(`its head is held by ${describe(held.signals)}`); 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 e4dcee2e..fa4746d0 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -404,7 +404,9 @@ async function dequeueSignaled( continue; } } - output.push(`dequeued ${number} ${head} ${found.signals.length} signals`); + output.push( + `${now.mergeQueueEntry != null ? "dequeued" : "disarmed"} ${number} ${head} ${found.signals.length} signals`, + ); } output.push(...(await sayUnsaid(github, repository, progress, saveProgress))); return output; diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index 12010e43..57690a1b 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -56,7 +56,7 @@ export function bodyFindings(body: string): string[] { const THREAD_COMMENT = "author { __typename login } authorAssociation body url pullRequestReview { id }"; const FIELDS = { - reviews: "id url author { __typename login } authorAssociation 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} } }`, diff --git a/src/cli/signals.ts b/src/cli/signals.ts index 09ed5c0c..3455988c 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -68,7 +68,9 @@ export function signals(pull: Activity, writers: ReadonlySet, carried?: reviewer: isBot(review.author) || writer(review.author) ? reviewerOf(review) : null, body: typeof review.body === "string" ? review.body : "", at: Date.parse(String(review.submittedAt)), - head: review.commit != null && heads.has(String(object(review.commit).oid)), + 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( @@ -108,12 +110,14 @@ export function signals(pull: Activity, writers: ReadonlySet, carried?: }); } - // Each reviewer's latest pass over the head lists what it still finds there; a pass may only open threads. + // 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 prior = latest.get(review.reviewer); - if (!prior || prior.at <= review.at) latest.set(review.reviewer, review); + 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), answered: false })) @@ -127,7 +131,13 @@ export function signals(pull: Activity, writers: ReadonlySet, carried?: 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 }); + // `bodyFindings` keys a repeated title with ` #`, which is not part of the finding. + found.push({ + kind: "finding", + url: pass.url, + author: pass.reviewer!, + text: title.replace(/ #\d+$/, ""), + }); const committed = Math.min(Date.parse(pull.committed), carried ? Date.parse(carried.picked) : Infinity); for (const decision of comments) { diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index fa0b5ac4..dc128ced 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -813,7 +813,7 @@ test("a queued or armed pick that draws an online finding leaves the queue, once github.now = Date.parse(at(4)); expect(await watchPass(REPOSITORY, { state }, deps(github))).toEqual([ `dequeued 30 ${HEAD} 1 signals`, - `dequeued 31 ${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"]); diff --git a/test/land.test.ts b/test/land.test.ts index daf5a364..2fb2ba68 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -4459,7 +4459,7 @@ describe("land fixer stop rules", () => { }); }); -test("a fixed head carries the heads its pick's rounds replaced, until a handback ends the pick", () => { +test("a fixed head carries the heads its pick's rounds replaced", () => { const state = tempDir(); const save = (record: object) => writeFileSync(historyPath(state, 5), JSON.stringify(record)); const round = (before: string, after?: string): Round => ({ @@ -4476,8 +4476,9 @@ test("a fixed head carries the heads its pick's rounds replaced, until a handbac expect(carriedBy(state, 5, "b")).toEqual({ heads: ["a"], picked: pick }); // A head no round put back, such as a writer's push, carries nothing. expect(carriedBy(state, 5, "d")).toBeUndefined(); - save({ pick, rounds: [round("a", "b"), round("b")] }); - expect(carriedBy(state, 5, "b")).toBeUndefined(); + // A handback leaves it, and a gate merge's pin is carried with it. + save({ pick, rounds: [{ ...round("m", "b"), pin: "a" }, round("b")] }); + expect(carriedBy(state, 5, "b")).toEqual({ heads: ["m", "a"], picked: pick }); // A pick that is not a time cannot bound decisions. save({ pick: "f".repeat(40), rounds: [round("a", "b")] }); expect(carriedBy(state, 5, "b")).toBeUndefined(); diff --git a/test/signals.test.ts b/test/signals.test.ts index 81d579d4..06ef3481 100644 --- a/test/signals.test.ts +++ b/test/signals.test.ts @@ -234,6 +234,18 @@ group("signals", () => { ]); }); + 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")], From d64b996315f73aed12e6639b669a6fa295e4bbb8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 15:48:40 -0800 Subject: [PATCH 16/21] fix(land): keep a handed-back fix's carry, carry a prepared head's pin, and key repeated titles apart Co-Authored-By: Claude Opus 5.5 --- src/cli/land-fixer.ts | 10 ++++++---- src/cli/land-watch-fix.ts | 13 ++++++++----- src/cli/online-review.ts | 9 ++++----- src/cli/signals.ts | 10 ++-------- test/land-watch.test.ts | 6 +++++- test/online-review.test.ts | 7 +++++-- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index faf5dcf1..234a6692 100644 --- a/src/cli/land-fixer.ts +++ b/src/cli/land-fixer.ts @@ -178,7 +178,7 @@ export interface Round { before: string; /** 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 PR went back to the queue at, which a fix or merge moves; a handback has none. */ + /** 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; @@ -1113,7 +1113,9 @@ async function collectJob( { outcome, failure }, ); if (rule) error = stopReason(rule, { outcome, failure }); - const pin = standing !== entry.head ? { pin: entry.head } : {}; + // 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, ...pin, failure, rule } : { @@ -1204,8 +1206,8 @@ 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" }); + // It keeps `after`: a head it published carries this pick's signals when picked again. + 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. diff --git a/src/cli/land-watch-fix.ts b/src/cli/land-watch-fix.ts index 7c355808..b40fe79c 100644 --- a/src/cli/land-watch-fix.ts +++ b/src/cli/land-watch-fix.ts @@ -685,8 +685,11 @@ 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" }); + // It keeps `after`: a head it published carries this pick's signals when picked again. + saveRound(state, number, history(state, number)!, { + ...(round ?? { job: name, before: entry.head, failure }), + outcome: "handback", + }); } if (handback) { job.handback = handback; @@ -715,9 +718,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, and a - * signal on `head` or on the `carried` pick it replaces hands it back too. + * 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, diff --git a/src/cli/online-review.ts b/src/cli/online-review.ts index 57690a1b..1cc99f20 100644 --- a/src/cli/online-review.ts +++ b/src/cli/online-review.ts @@ -31,10 +31,9 @@ export function classify(body: string): Answer { /** * 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 keyed by its - * title and how many earlier lines of the body share that title. + * 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( @@ -50,7 +49,7 @@ export function bodyFindings(body: string): string[] { .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 }; }); } @@ -249,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 }); diff --git a/src/cli/signals.ts b/src/cli/signals.ts index 3455988c..04c38a94 100644 --- a/src/cli/signals.ts +++ b/src/cli/signals.ts @@ -120,7 +120,7 @@ export function signals(pull: Activity, writers: ReadonlySet, carried?: if (!prior || prior.at <= review.at) latest.set(key, review); } const passes = [...latest.values()] - .map((pass) => ({ ...pass, titles: bodyFindings(pass.body), answered: false })) + .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. @@ -131,13 +131,7 @@ export function signals(pull: Activity, writers: ReadonlySet, carried?: for (const pass of passes) if (!pass.answered) for (const title of pass.titles) - // `bodyFindings` keys a repeated title with ` #`, which is not part of the finding. - found.push({ - kind: "finding", - url: pass.url, - author: pass.reviewer!, - text: title.replace(/ #\d+$/, ""), - }); + 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) { diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index dc128ced..443d0337 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -12,7 +12,7 @@ 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"; @@ -1378,6 +1378,10 @@ describe.skipIf(process.platform === "win32")("the watcher's land fixer", () => 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"); + // 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]); }); test("a fix queued without its finding still carries the dropped head's, and leaves the queue once one arrives", async () => { diff --git a/test/online-review.test.ts b/test/online-review.test.ts index b665a9d5..f4c66d98 100644 --- a/test/online-review.test.ts +++ b/test/online-review.test.ts @@ -43,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", () => { @@ -56,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", From 75ece8f94e6c497b16ec99c9ce57b29fd8d4a950 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 16:16:34 -0800 Subject: [PATCH 17/21] fix(land): give the carry its own record, written before the fixer publishes a head Co-Authored-By: Claude Opus 5.5 --- README.md | 5 ++-- src/cli/land-fixer.ts | 45 ++++++++++++++++++++++----------- src/cli/land-watch-fix.ts | 1 - test/land-watch.test.ts | 6 ++++- test/land.test.ts | 52 ++++++++++++++++++++++++++++++--------- 5 files changed, 79 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index b790c6c6..09e41cda 100644 --- a/README.md +++ b/README.md @@ -893,8 +893,9 @@ 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 and even after a handback. Only a writer's -push ends the carry. +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. diff --git a/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index 234a6692..09f609b0 100644 --- a/src/cli/land-fixer.ts +++ b/src/cli/land-fixer.ts @@ -209,22 +209,32 @@ 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 up to the - * round that put `head` back in the queue, with the pins the gate's base merges stood on, and when the pick was made. - * A handback leaves the carry, 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. + * 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 record = history(state, number); - if (!record || !Number.isFinite(Date.parse(record.pick))) return undefined; - let end = record.rounds.length; - while (end > 0 && record.rounds[end - 1]!.after !== head) end--; - if (end === 0) return undefined; - return { - heads: record.rounds.slice(0, end).flatMap((round) => (round.pin ? [round.before, round.pin] : [round.before])), - picked: record.pick, - }; + 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 { + if (!round.after || round.after === round.before || !Number.isFinite(Date.parse(pick))) return; + const path = carryPath(state, number, round.after); + if (existsSync(path)) return; + // The head it replaced may carry an earlier pick's signals, which picking it again answered none of. + const earlier = carriedBy(state, number, round.before); + const heads = [...(earlier?.heads ?? []), round.before, ...(round.pin ? [round.pin] : [])]; + const picked = earlier && Date.parse(earlier.picked) < Date.parse(pick) ? earlier.picked : pick; + save(path, { heads: [...new Set(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 { @@ -239,6 +249,7 @@ export function carriedOf(state: string, pulls: ObjectValue[]): Map item.job !== round.job), round] }; + saveCarry(state, number, record.pick, round); save(historyPath(state, number), next); return next; } @@ -1145,6 +1156,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); @@ -1206,7 +1224,6 @@ 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") { - // It keeps `after`: a head it published carries this pick's signals when picked again. saveRound(state, number, history(state, number)!, { ...round, outcome: "handback" }); } if (error) { diff --git a/src/cli/land-watch-fix.ts b/src/cli/land-watch-fix.ts index b40fe79c..ee25f8b8 100644 --- a/src/cli/land-watch-fix.ts +++ b/src/cli/land-watch-fix.ts @@ -685,7 +685,6 @@ 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") { - // It keeps `after`: a head it published carries this pick's signals when picked again. saveRound(state, number, history(state, number)!, { ...(round ?? { job: name, before: entry.head, failure }), outcome: "handback", diff --git a/test/land-watch.test.ts b/test/land-watch.test.ts index 443d0337..a37c2f61 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -15,7 +15,7 @@ import { CHECK_LOG } from "../src/cli/land-checks"; 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, @@ -1382,6 +1382,10 @@ describe.skipIf(process.platform === "win32")("the watcher's land fixer", () => 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 queued without its finding still carries the dropped head's, and leaves the queue once one arrives", async () => { diff --git a/test/land.test.ts b/test/land.test.ts index e968e46b..fad142d4 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -36,6 +36,7 @@ import { history, historyPath, redactTokens, + saveRound, type Round, stopReason, stopRule, @@ -3332,6 +3333,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`, @@ -4484,27 +4502,37 @@ describe("land fixer stop rules", () => { test("a fixed head carries the heads its pick's rounds replaced", () => { const state = tempDir(); - const save = (record: object) => writeFileSync(historyPath(state, 5), JSON.stringify(record)); - const round = (before: string, after?: string): Round => ({ - job: before, + 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(); - save({ pick, rounds: [round("a", "b"), round("b", "c")] }); - expect(carriedBy(state, 5, "c")).toEqual({ heads: ["a", "b"], picked: pick }); - expect(carriedBy(state, 5, "b")).toEqual({ heads: ["a"], picked: pick }); + 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 leaves it, and a gate merge's pin is carried with it. - save({ pick, rounds: [{ ...round("m", "b"), pin: "a" }, round("b")] }); - expect(carriedBy(state, 5, "b")).toEqual({ heads: ["m", "a"], picked: pick }); - // A pick that is not a time cannot bound decisions. - save({ pick: "f".repeat(40), rounds: [round("a", "b")] }); - expect(carriedBy(state, 5, "b")).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 }); + // 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. */ From e4776cf76e87eb617ec97c08718f761faadc4484 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 16:45:50 -0800 Subject: [PATCH 18/21] fix(land): chain a gate merge's pin carry, ask contents: write to disarm, and say only the mutations taken Co-Authored-By: Claude Opus 5.5 --- README.md | 12 +++++++----- src/cli/land-fixer.ts | 22 +++++++++++++++------- src/cli/land-watch.ts | 5 +++-- test/land-watch.test.ts | 2 +- test/land.test.ts | 5 +++++ 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 09e41cda..7492a056 100644 --- a/README.md +++ b/README.md @@ -913,7 +913,8 @@ 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, 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. With `--fixer`, it also runs the land fixer on each PR the +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 @@ -1043,10 +1044,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/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index 09f609b0..807aa7bc 100644 --- a/src/cli/land-fixer.ts +++ b/src/cli/land-fixer.ts @@ -227,14 +227,22 @@ export function saveCarry( pick: string, round: Pick, ): void { - if (!round.after || round.after === round.before || !Number.isFinite(Date.parse(pick))) return; - const path = carryPath(state, number, round.after); + const after = round.after; + if (!after || !Number.isFinite(Date.parse(pick))) return; + const path = carryPath(state, number, after); if (existsSync(path)) return; - // The head it replaced may carry an earlier pick's signals, which picking it again answered none of. - const earlier = carriedBy(state, number, round.before); - const heads = [...(earlier?.heads ?? []), round.before, ...(round.pin ? [round.pin] : [])]; - const picked = earlier && Date.parse(earlier.picked) < Date.parse(pick) ? earlier.picked : pick; - save(path, { heads: [...new Set(heads)], picked } satisfies Carried); + // 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 { diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index fa4746d0..e94ab13a 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -360,7 +360,7 @@ async function dequeueSignaled( 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 and disarming its auto-merge" : "disarming its auto-merge"}, 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.`, + 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(); @@ -914,7 +914,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/test/land-watch.test.ts b/test/land-watch.test.ts index a37c2f61..cb1dc7b2 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -963,7 +963,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: { diff --git a/test/land.test.ts b/test/land.test.ts index fad142d4..a415291a 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -4528,6 +4528,11 @@ test("a fixed head carries the heads its pick's rounds replaced", () => { 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")); From edc59ab2c0e23435bbfe1bc38441e38f83c1f609 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 16:50:15 -0800 Subject: [PATCH 19/21] docs(land): say the non-fixer watcher's token disarms with contents: write Co-Authored-By: Claude Opus 5.5 --- src/cli/land-watch.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/land-watch.ts b/src/cli/land-watch.ts index e94ab13a..0019230f 100644 --- a/src/cli/land-watch.ts +++ b/src/cli/land-watch.ts @@ -882,8 +882,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 From 10c1b4660e4b40f5e341b66273b381e2f7b03429 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 13:32:49 -0800 Subject: [PATCH 20/21] test: adapt the signal tests to main's land request and fixer context Co-Authored-By: Claude Opus 5.5 --- test/hold.test.ts | 8 +++++--- test/land.test.ts | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/hold.test.ts b/test/hold.test.ts index cf98d061..ff212c06 100644 --- a/test/hold.test.ts +++ b/test/hold.test.ts @@ -414,13 +414,13 @@ describe("merge --land", () => { ], }, }; - expect(await refused(land(PR, HEAD, "merge", pull.deps()))).toContain( + 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, HEAD, "merge", pull.deps()); + await land(PR, { sha: HEAD, method: "merge", check: clean }, pull.deps()); expect(pull.writes).not.toEqual([]); }); @@ -444,7 +444,9 @@ describe("merge --land", () => { }, }; }; - expect(await refused(land(PR, HEAD, "merge", pull.deps(), true))).toContain("Maintainer decision: late."); + expect( + await refused(land(PR, { sha: HEAD, method: "merge", direct: true, check: clean }, pull.deps())), + ).toContain("Maintainer decision: late."); expect(pull.writes).toEqual([]); }); diff --git a/test/land.test.ts b/test/land.test.ts index 54227f21..e259aeaa 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -5043,6 +5043,7 @@ describe.skipIf(process.platform === "win32")("the watcher's own queue", () => { fixer: wrapper, jobs: 1, effort: "medium", + branches: [], tags: [], timeout: 60, token: queue.token, From 9bef64c7787f2f8f4326a59364111740bf04538c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 13:43:23 -0800 Subject: [PATCH 21/21] fix(land): mark a fixer's signal handback unanswered_signal, reread before disarming, and recheck signals before queueing Co-Authored-By: Claude Opus 5.5 --- src/cli/hold.ts | 6 +++- src/cli/land-fixer.ts | 16 +++++++++-- src/cli/land-watch-fix.ts | 15 +++++++--- test/hold.test.ts | 31 ++++++++++++++++++++ test/land-watch.test.ts | 60 +++++++++++++++++++++++++++++++++++++++ test/land.test.ts | 1 + 6 files changed, 121 insertions(+), 8 deletions(-) diff --git a/src/cli/hold.ts b/src/cli/hold.ts index 37b693bf..58638c6e 100644 --- a/src/cli/hold.ts +++ b/src/cli/hold.ts @@ -281,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; } @@ -288,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()); diff --git a/src/cli/land-fixer.ts b/src/cli/land-fixer.ts index 44d55610..5e15aa22 100644 --- a/src/cli/land-fixer.ts +++ b/src/cli/land-fixer.ts @@ -61,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 {} @@ -153,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. */ @@ -1196,7 +1200,7 @@ async function collectJob( ).get(number)!; if (held.head !== head) throw new FixerRefusal("head moved before it was re-enqueued"); if (held.signals.length > 0) - throw new FixerRefusal(`its head is held by ${describe(held.signals)}`); + 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) { @@ -1213,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`; } } @@ -1268,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-watch-fix.ts b/src/cli/land-watch-fix.ts index 08d75c1b..29ab9508 100644 --- a/src/cli/land-watch-fix.ts +++ b/src/cli/land-watch-fix.ts @@ -17,6 +17,7 @@ import { comment, conflictFailure, FixerRefusal, + SignalRefusal, handbackLog, fixerMarkdown, fixerText, @@ -100,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. */ @@ -712,6 +715,7 @@ async function collect( if (!(failure instanceof FixerRefusal)) throw failure; handback = failure.message; if (failure.resolution) job.resolution = failure.resolution; + if (failure instanceof SignalRefusal) job.signaled = true; } } @@ -743,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); @@ -780,16 +784,19 @@ async function requeue( ).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) + if (pull.mergeQueueEntry != null) { await github.graphql("mutation($id: ID!) { dequeuePullRequest(input: { id: $id }) { clientMutationId } }", { id, }); - if (queued(pull, dropped) && pull.autoMergeRequest != null) + // 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 FixerRefusal(`its head is held by ${describe(held.signals)}`); + throw new SignalRefusal(`its head is held by ${describe(held.signals)}`); } if (!queued(pull, dropped)) { try { diff --git a/test/hold.test.ts b/test/hold.test.ts index ff212c06..c4e0b59c 100644 --- a/test/hold.test.ts +++ b/test/hold.test.ts @@ -450,6 +450,37 @@ describe("merge --land", () => { 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 275598a8..4e43846e 100644 --- a/test/land-watch.test.ts +++ b/test/land-watch.test.ts @@ -243,6 +243,15 @@ function deps(world: World): GitHubDependencies { 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; @@ -1395,6 +1404,8 @@ describe.skipIf(process.platform === "win32")("the watcher's land fixer", () => 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); @@ -1405,6 +1416,55 @@ describe.skipIf(process.platform === "win32")("the watcher's land fixer", () => 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(); diff --git a/test/land.test.ts b/test/land.test.ts index e259aeaa..70daf6cc 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -5058,6 +5058,7 @@ describe.skipIf(process.platform === "win32")("the watcher's own queue", () => { 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(""); }); });