From 33fa0f425b1ec556d93492fdfbd3af2020684d3c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 16:06:46 +0000 Subject: [PATCH] fix(land): eject a conflicting PR only once the chain before it lands (#289) * fix(land): eject a conflicting PR only once the chain before it lands push ejected every PR that conflicted with the chain built before it as merge_conflict, even when that chain did not land in the run: a passing prefix that stopped short of it, a moved head, a withdrawn PR, or a refused push. The PR then lost its place for a conflict with PRs that never reached the base. Now push ejects it only once every PR built before it has landed, which includes a conflict with the base alone, and the comment names the PRs that landed. Otherwise it stays queued, and the next run builds it again without the PRs that did not land. check names what each PR conflicts with. The rechaining after a PR fails alone (#287) already covers the moq-dev/moq queue #6985, #7027, #6999; its test now uses that shape. Co-authored-by: Claude Opus 5.5 --- README.md | 5 ++- src/cli/land.ts | 88 ++++++++++++++++++++++++++++++----------------- src/cli/main.ts | 6 ++-- test/land.test.ts | 79 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 138 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 57437583..50551b18 100644 --- a/README.md +++ b/README.md @@ -516,13 +516,16 @@ both in one push would close the PRs unmerged. A moved head stops the landing, and the prefix before it lands only if a check passed on exactly that prefix, since a later PR can mask an earlier one's failure. A failed landing moves the branches back, so they stay queued. The rest of the queue waits for the next run. +A PR that conflicts with the chain before it is ejected only once that chain +lands, so with nothing before it, at once. Otherwise it stays queued, and the +next run builds it again without the PRs that did not land. An ejected PR loses its auto-merge and `quest:merge` label and gets a comment with the reason, which `quest status` prints under `landing-stopped`: | Reason | Why | | --- | --- | -| `merge_conflict` | It conflicts with the base or the PRs before it, beyond what `quest resolve` settles. | +| `merge_conflict` | It conflicts with the base, or with the PRs that landed before it, beyond what `quest resolve` settles. | | `failed_checks` | The check failed with it alone on the base, or a required check failed on its head. | | `checks_pending` | A required check had not passed on its head. | | `review_required`, `changes_requested` | Its review decision blocks it. | diff --git a/src/cli/land.ts b/src/cli/land.ts index bea16b6f..e4b82bc6 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -447,10 +447,17 @@ interface Link { tree: string; } +/** A queued PR that conflicts with the chain built before it, which holds `after` links. */ +interface Conflict { + entry: Entry; + files: string[]; + after: number; +} + /** The queue built onto the base, in order, without the PRs that conflict. */ interface Chain { links: Link[]; - conflicts: { entry: Entry; files: string[] }[]; + conflicts: Conflict[]; } /** @@ -482,7 +489,7 @@ function build(root: string, plan: Plan): Chain { const files = lines(git(dir, ["diff", "--name-only", "--diff-filter=U"])); if (files.length === 0) throw new Error(`git merge --squash ${entry.head} failed: ${merged.stderr.trim()}`); - chain.conflicts.push({ entry, files }); + chain.conflicts.push({ entry, files, after: chain.links.length }); git(dir, ["reset", "--quiet", "--hard", "HEAD"]); continue; } @@ -559,14 +566,19 @@ export async function checkQueue( deadline?: number, ): Promise { const checks: Tested[] = []; - const reported = new Set(); + const reported = new Set(); let queue = plan.queue; for (;;) { const chain = build(root, { ...plan, queue }); - for (const { entry, files } of chain.conflicts) { - if (reported.has(entry.number)) continue; - reported.add(entry.number); - stderr(`quest land: #${entry.number} conflicts with the PRs before it in ${files.join(", ")}\n`); + for (const conflict of chain.conflicts) { + const before = chain.links.slice(0, conflict.after).map((link) => `#${link.entry.number}`); + const line = + before.length === 0 + ? `#${conflict.entry.number} conflicts with the base in ${conflict.files.join(", ")}` + : `#${conflict.entry.number} conflicts with ${before.join(" ")} before it in ${conflict.files.join(", ")}; it stays queued unless they land`; + if (reported.has(line)) continue; + reported.add(line); + stderr(`quest land: ${line}\n`); } let failed: Entry | undefined; for (let count = chain.links.length; count > 0; count = Math.ceil(count / 2)) { @@ -609,7 +621,8 @@ function execute(cwd: string, command: string[], stderr: (text: string) => void) * what cannot land, then land the prefix that passed: move each PR's branch to * its squash commit with a lease on the head that was tested, and fast-forward * the base to the last one. A head that moved stops the landing; the prefix - * before it lands only if a check passed on exactly that prefix. + * before it lands only if a check passed on exactly that prefix. A PR that + * conflicts is ejected only once the chain before it has landed, if any. */ export async function pushQueue( root: string, @@ -621,13 +634,6 @@ export async function pushQueue( const { chain, failed } = replay(root, plan, result); const ejections = [...plan.eject]; const ejectLink = (entry: Entry, reason: Reason, detail: string) => ejections.push(ejection(entry, reason, detail)); - for (const { entry, files } of chain.conflicts) { - ejectLink( - entry, - "merge_conflict", - `it conflicts with the base or the PRs queued before it in ${files.join(", ")}; merge the base, push, and land it again`, - ); - } for (const entry of failed) { ejectLink( entry, @@ -676,28 +682,46 @@ export async function pushQueue( } const github = new GitHub(deps); - for (const ejection of ejections) { + const report = async (ejection: Ejection) => { if (await eject(github, plan, ejection, run)) output.push(`ejected ${ejection.number} ${ejection.reason}`); - } - if (count === 0) return output; + }; + for (const ejection of ejections) await report(ejection); - const links = chain.links.slice(0, count); - const refused = await landLinks(root, plan, links, deps); - if (refused === null) { - for (const link of links) output.push(`landed ${link.entry.number} ${link.commit}`); - return output; - } - // A refused push stops the landing; it lands again on the next run, without the PR that stopped it. - const stop = async (entry: Entry, reason: Reason, detail: string) => { - if (await eject(github, plan, ejection(entry, reason, detail), run)) { - output.push(`ejected ${entry.number} ${reason}`); + let landed = 0; + if (count > 0) { + const links = chain.links.slice(0, count); + const refused = await landLinks(root, plan, links, deps); + if (refused === null) { + for (const link of links) output.push(`landed ${link.entry.number} ${link.commit}`); + landed = count; + } else { + // A refused push stops the landing; it lands again on the next run, without the PR that stopped it. + await report(stopper(root, plan, refused)); } - return output; - }; + } + // A PR that conflicts with the chain before it conflicts with the base once that chain lands. Until + // then it stays queued: the next run builds it again without the PRs that did not land. + for (const { entry, files, after } of chain.conflicts) { + if (after > landed) continue; + const before = chain.links.slice(0, after).map((link) => `#${link.entry.number}`); + const now = before.length === 0 ? "" : ` now that ${before.join(", ")} landed before it`; + await report( + ejection( + entry, + "merge_conflict", + `it conflicts with the base${now} in ${files.join(", ")}; merge the base, push, and land it again`, + ), + ); + } + return output; +} + +/** The PR that stopped a refused landing push, to eject; throws when none did. */ +function stopper(root: string, plan: Plan, refused: { links: Link[]; stderr: string }): Ejection { for (const { entry } of refused.links) { const head = remoteHead(root, remoteOf(plan, entry), entry.branch); if (head !== entry.head) { - return stop( + return ejection( entry, "head_moved", `its head moved from ${entry.head} to ${head ?? "nothing"} while the gate checked it`, @@ -706,7 +730,7 @@ export async function pushQueue( } const [first] = refused.links; if (refused.links.length === 1 && !local(plan, first.entry)) { - return stop(first.entry, "push_failed", `the gate could not move its head: ${refused.stderr}`); + return ejection(first.entry, "push_failed", `the gate could not move its head: ${refused.stderr}`); } throw new Error(`the landing push failed, so nothing landed: ${refused.stderr}`); } diff --git a/src/cli/main.ts b/src/cli/main.ts index 6f30a036..87fdcc40 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -588,10 +588,12 @@ export function main(argv: string[], out: Output): number | Promise { .summary("Land the prefix that passed and eject what cannot land") .description( "Rebuild the plan's chains, refuse a result that did not test exactly their prefixes, then eject the " + - "PRs the plan names, those that conflict, and those that failed alone. Land the prefix that passed: " + + "PRs the plan names and those that failed alone. Land the prefix that passed: " + "move each PR's branch with a lease on its tested head (a fork's first, the rest in one atomic push " + "with the base's fast-forward). A moved head stops the landing; the prefix before it lands only if " + - "a check passed on exactly that prefix. A failed landing moves fork branches back. Ejection " + + "a check passed on exactly that prefix. A failed landing moves fork branches back. A PR that " + + "conflicts with the chain before it is ejected once that chain lands, so with nothing before it, at " + + "once; otherwise it stays queued for the next run. Ejection " + "comments link the GitHub Actions run when GITHUB_RUN_ID is set. Prints `landed ` " + "and `ejected ` lines; any line means the queue changed, so a workflow can run " + "the gate again. Needs push access through origin and to forks that allow maintainer edits, and " + diff --git a/test/land.test.ts b/test/land.test.ts index 11370afc..66cfedea 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -325,12 +325,16 @@ describe.skipIf(process.platform === "win32")("quest land", () => { const clash = forge.pull("Two is II", { "code.txt": "one\nII\nthree\n" }); const { checked, landing } = await forge.round(); expect(checked).toEqual([[1, 2, 3]]); - expect(landing.slice(0, 1)).toEqual(["ejected 4 merge_conflict"]); - expect(landing.slice(1).map((line) => line.split(" ")[1])).toEqual(["1", "2", "3"]); + expect(landing.slice(0, 3).map((line) => line.split(" ").slice(0, 2).join(" "))).toEqual([ + "landed 1", + "landed 2", + "landed 3", + ]); + expect(landing.slice(3)).toEqual(["ejected 4 merge_conflict"]); expect(git(forge.origin, "show", "main:quest/m0/README.md")).toBe(EPIC(["a", "b", "c"]).trim()); expect(clash.landing).toBe(false); expect(clash.labels).toEqual([]); - expect(forge.comment(4)).toContain("in code.txt"); + expect(forge.comment(4)).toContain("conflicts with the base now that #1, #2, #3 landed before it in code.txt"); expect(forge.comment(4)).toContain(`([run](${RUN}))`); expect(forge.comment(4)).toMatch(/$/); }); @@ -385,15 +389,80 @@ describe.skipIf(process.platform === "win32")("quest land", () => { }); test("a PR that conflicted only with an ejected one joins the rest", async () => { + // moq-dev/moq's queue #6985, #7027, #6999: #7027 conflicted only with #6985, which failed alone. const forge = new Forge(); forge.pull("Two is 2, broken", { "code.txt": "one\n2\nthree\n", broken: "\n" }); forge.pull("Two is II", { "code.txt": "one\nII\nthree\n" }); + forge.pull("Three", { "3.txt": "3\n" }); const { checked, landing } = await forge.round(); - expect(checked).toEqual([[1], [2]]); - expect(landing).toEqual(["ejected 1 failed_checks", `landed 2 ${forge.main()}`]); + expect(checked).toEqual([[1, 3], [1], [2, 3]]); + expect(landing.map((line) => line.split(" ").slice(0, 2).join(" "))).toEqual([ + "ejected 1", + "landed 2", + "landed 3", + ]); + expect(landing[0]).toBe("ejected 1 failed_checks"); expect(git(forge.origin, "show", "main:code.txt")).toBe("one\nII\nthree"); }); + test("a PR that conflicts with one ejected after the budget is spent stays queued", async () => { + const forge = new Forge(); + forge.pull("Two is 2, broken", { "code.txt": "one\n2\nthree\n", broken: "\n" }); + const clash = forge.pull("Two is II", { "code.txt": "one\nII\nthree\n" }); + const first = await forge.round(0); + expect(first.checked).toEqual([[1]]); + expect(first.landing).toEqual(["ejected 1 failed_checks"]); + expect(clash.landing).toBe(true); + const second = await forge.round(0); + expect(second.checked).toEqual([[2]]); + expect(second.landing).toEqual([`landed 2 ${forge.main()}`]); + }); + + test("a PR that conflicts with one that did not land stays queued for the next run", async () => { + const forge = new Forge(); + forge.pull("One", { "1.txt": "1\n" }); + forge.pull("Two is 2, broken", { "code.txt": "one\n2\nthree\n", broken: "\n" }); + const clash = forge.pull("Two is II", { "code.txt": "one\nII\nthree\n" }); + // The passing prefix #1 lands at once, so #2 is never checked alone, and #3 waits for it. + const first = await forge.round(); + expect(first.checked).toEqual([[1, 2], [1]]); + expect(first.landing.map((line) => line.split(" ").slice(0, 2).join(" "))).toEqual(["landed 1"]); + expect(clash.landing).toBe(true); + expect(forge.comment(3)).toBeUndefined(); + const second = await forge.round(); + expect(second.checked).toEqual([[2], [3]]); + expect(second.landing).toEqual(["ejected 2 failed_checks", `landed 3 ${forge.main()}`]); + }); + + test("a PR that conflicts with one whose head moved stays queued", async () => { + const forge = new Forge(); + const base = forge.main(); + const a = forge.pull("Two is 2", { "code.txt": "one\n2\nthree\n" }); + const clash = forge.pull("Two is II", { "code.txt": "one\nII\nthree\n" }); + const deps = await forge.deps(); + const plan = await resolveQueue(REPOSITORY, undefined, deps); + const result = await checkQueue(forge.clone(), plan, CHECK, () => undefined); + expect(result.checks.map((tested) => tested.numbers)).toEqual([[1]]); + const moved = git( + forge.origin, + "-c", + "user.name=Late", + "-c", + "user.email=late@example.test", + "commit-tree", + "-p", + a.head!, + "-m", + "late", + `${a.head}^{tree}`, + ); + git(forge.origin, "update-ref", "refs/heads/topic-1", moved); + expect(await pushQueue(forge.clone(), plan, result, RUN, deps)).toEqual(["ejected 1 head_moved"]); + expect(forge.main()).toBe(base); + expect(clash.landing).toBe(true); + expect(forge.comment(2)).toBeUndefined(); + }); + test("a moved head stops the landing, and an earlier prefix never checked alone does not land", async () => { const forge = new Forge(); const base = forge.main();