Public: anyone can read this; the crew can change it.

A pull's check runs on the merge of its head into the current base; a conflict is a named failure #12547

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/12533-a-pull-s-check-runs-on-the-merge-of-its → main 8 commits fork 2 threads · 2 unresolved

  • Merged. scout/12533-a-pull-s-check-runs-on-the-merge-of-its was merged into main as 41b8a6d.

Review

Sign in to review this change.
7 files changed +333 −5
docs/PULLS.md +2

Diff not shown — a line 2,393 characters wide (limit 2,000). +2

View file
src/checks/base_moved.ts added +68
@@ -0,0 +1,68 @@
1 /**
2 * Whether a pull's check is asked again because its base moved (#12533).
3 * A pull's check is of its head merged into the base's tip (`merged.ts`) and
4 * records that tip (`base_oid`); once the base moves, a green is of a tree
5 * that no longer lands, so it is checked again by itself.
6 *
7 * At most once per base change: a check queued or running at this head is
8 * never stopped or re-queued by a move (it merges whatever tip is newest
9 * when it starts), and a head already checked against this tip is not
10 * checked against it again. A fast-moving base therefore costs one waiting
11 * check per pull, never a queue that grows with every push.
12 */
13
14 import type { Run } from "../core/runs.ts";
15
16 /** The reason a check asked because the base moved carries. */
17 export const BASE_MOVED = "base moved";
18
19 export function recheckOnBaseMove(
20 /** The runs at the pull's head, newest first (`checksForHead`). */
21 runs: readonly Run[],
22 /** The environment the pull's checks run in, `pull-<n>`. */
23 env: string,
24 /** The base branch's new tip. */
25 baseTip: string,
26 /** Whether the pull lands through the merge queue's gate, which merges with the tip itself. */
27 landGated: boolean,
28 ): boolean {
29 if (landGated || baseTip === "") return false;
30 const checks = runs.filter((r) => r.kind === "check" && r.env === env);
31 const latest = checks[0];
32 if (latest === undefined) return false;
33 if (checks.some((r) => r.state === "queued" || r.state === "running")) return false;
34 // Only a green is held to the new base: a red stays the builder's to fix.
35 if (latest.state !== "ok") return false;
36 // A check that merged nothing (a kept fork's combined commit) says nothing about the tip.
37 if (latest.baseOid === "") return false;
38 return !checks.some((r) => r.baseOid === baseTip);
39 }

This decides whether to re-check, but not in what order. Box 2 asks for the newest base first; the order is left to the caller and nothing tests it.

40
41 /**
42 * The order a move's re-checks take slots in: newest base first (#12533).
43 * A check merges the base's tip when it starts (`merged.ts`), so the green
44 * that started latest is of the newest base, the one nearest the tip and the
45 * likeliest still to land; it goes first. Ties go to the lower pull number.
46 * The check queue starts the newest request first (`pickNext`), so the
47 * caller asks for these last-first, with the queue held until all are asked.
48 * An approved or owner-ranked pull still goes before, as everywhere.
49 */
50 export function newestBaseFirst<T extends { readonly number: number; readonly green: Run }>(
51 due: readonly T[],
52 ): T[] {
53 const started = (item: T): string => item.green.startedAt ?? item.green.createdAt;
54 return [...due].sort((a, b) =>
55 started(a) > started(b) ? -1 : started(a) < started(b) ? 1 : a.number - b.number,
56 );
57 }
58
59 /** The green a move holds to the new tip, when it holds one (`recheckOnBaseMove`). */
60 export function dueGreen(
61 runs: readonly Run[],
62 env: string,
63 baseTip: string,
64 landGated: boolean,
65 ): Run | undefined {
66 if (!recheckOnBaseMove(runs, env, baseTip, landGated)) return undefined;
67 return runs.find((r) => r.kind === "check" && r.env === env);
68 }
src/checks/merged.ts +1 −1
@@ -49,7 +49,7 @@ export async function mergeForCheck(
4949 if (tree.error.kind === "conflict")
5050 return {
5151 ok: false,
52 error: `${NO_MERGE} ${baseBranch}: ${tree.error.files.join(", ") || "(paths unknown)"}`,
52 error: `${NO_MERGE} ${baseBranch}: conflicts in ${tree.error.files.join(", ") || "(paths unknown)"}`,
5353 };
5454 return { ok: false, error: "the pull could not be merged with its base" };
5555 }
src/checks/service.ts +76 −2
@@ -23,7 +23,7 @@ import { activeCrewRuns, crewHoldsCore, forgeCpus, holdClock } from "../core/one
2323 import { announceRun } from "../actions/runs.ts";
2424 import { announceShard } from "./shard_event.ts";
2525 import type { ForgeEvent } from "../core/events.ts";
26 import { getPull, listPulls, type Pull } from "../core/pulls.ts";
26 import { getPull, listOpenPulls, listPulls, type Pull } from "../core/pulls.ts";
2727 import { isContributionFork } from "../core/contribute.ts";
2828 import { issueMayRun } from "../core/pull_trust.ts";
2929 import { outsideAgentWaits } from "../origins/guest.ts";
@@ -62,6 +62,7 @@ import { readCombined } from "../git/combined.ts";
6262 import { approvedOnce } from "./approved_first.ts";
6363 import { outsideOnce, ownersFirst } from "./outside_lane.ts";
6464 import { checkOnApproval } from "./approval.ts";
65 import { BASE_MOVED, dueGreen, newestBaseFirst } from "./base_moved.ts";
6566 import { checkKey, provenBy, recordProven } from "./proven.ts";
6667 import { beginGate, endGate, gateHolding } from "./gate_record.ts";
6768 import { giveHistory } from "./history.ts";
@@ -531,6 +532,8 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
531532 const slotted = (): number => inFlight.size - (verifying === undefined ? 0 : 1);
532533 const settle = options.landSettleMs ?? 0;
533534 let stopped = false;
535 /** While above 0, a batch of requests is being queued and `schedule` waits for its end. */
536 let holding = 0;
534537
535538 sweepSoon(app);
536539
@@ -636,6 +639,11 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
636639 untrackRun(run.id, abort);
637640 inFlight.delete(run.id);
638641 if (verifying === run.id) verifying = undefined;
642 // A pull's green that ran while its base moved is of the old tip: asked again once.
643 if (run.issueId !== null && !stopped)
644 void recheckFinished(run.id, repo).catch((error) =>
645 log.error("check could not be asked again after the base moved", { run: run.id, error: String(error) }),
646 );
639647 // The slot this run held is free again: give it to whichever queued
640648 // check now deserves it most, not whoever asked first.
641649 schedule();
@@ -706,7 +714,7 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
706714 const pullHold = holdClock(options.holdMaxMs);
707715
708716 function schedule(): void {
709 if (stopped) return;
717 if (stopped || holding > 0) return;
710718 const limit = capacity();
711719 if (options.gate === undefined) gate.resize(limit);
712720 // On one CPU a pull's check waits while a crew run works, for at most
@@ -1747,6 +1755,7 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
17471755 undefined,
17481756 actor,
17491757 );
1758 await recheckBase(repo, branch, update.newOid);
17501759 for (const pull of listPulls(app.db, repo.id, {
17511760 sealed: true,
17521761 state: "open",
@@ -1865,6 +1874,8 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
18651874 actor,
18661875 settle > 0 ? new Date(Date.now() + settle).toISOString() : undefined,
18671876 );
1877 if (event.kind === "pull.merged" && typeof mergeOid === "string" && /^[0-9a-f]{40}$/.test(mergeOid))
1878 await recheckBase(repo, pull.baseBranch, mergeOid);
18681879 return;
18691880 }
18701881 default:
@@ -1872,6 +1883,69 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
18721883 }
18731884 }
18741885
1886 /**
1887 * The base moved (#12533): every open pull on it whose green was of an
1888 * older tip is checked again, once per move (`base_moved.ts`), newest base
1889 * first (`newestBaseFirst`). A queued check merges the newest tip when it
1890 * starts, so a later move supersedes nothing and queues nothing. Skipped
1891 * where the merge queue gates lands: its gate merges with the tip itself.
1892 */
1893 async function recheckBase(repo: Repo, branch: string, tip: string): Promise<void> {
1894 if (landGated(repo)) return;
1895 const due: { number: number; green: Run; pull: Pull }[] = [];
1896 for (const pull of listOpenPulls(app.db, repo.id)) {
1897 if (pull.baseBranch !== branch) continue;
1898 const green = dueOf(pull, tip);
1899 if (green !== undefined) due.push({ number: pull.number, green, pull });
1900 }
1901 await askInOrder(repo, newestBaseFirst(due).map((item) => item.pull));
1902 }
1903
1904 const landGated = (repo: Repo): boolean => repo.autonomy === "autonomous" && repo.autonomyBy !== null;
1905
1906 function dueOf(pull: Pull, tip: string): Run | undefined {
1907 if (pull.state !== "open" || pull.headOid === "" || pull.mergedAt !== null) return undefined;
1908 return dueGreen(checksForHead(app.db, pull.id, pull.headOid), `pull-${pull.number}`, tip, false);
1909 }
1910
1911 /**
1912 * Asks a re-check for each pull, the first to take a slot first. The queue
1913 * starts the newest request first (`pickNext`), so they are asked last-first
1914 * with scheduling held until every one is queued: a free slot does not go
1915 * to whichever happened to be asked before the rest.
1916 */
1917 async function askInOrder(repo: Repo, pulls: readonly Pull[]): Promise<void> {
1918 if (pulls.length === 0) return;
1919 holding++;
1920 try {
1921 for (const pull of [...pulls].reverse()) {
1922 try {
1923 await request(repo, `pull-${pull.number}`, pull.headOid, BASE_MOVED, pull.id);
1924 } catch (error) {
1925 log.error("check could not be asked again after the base moved", { pull: pull.number, error: String(error) });
1926 }
1927 }
1928 } finally {
1929 holding--;
1930 schedule();
1931 }
1932 }
1933
1934 /** A check that finished green while its base moved is asked again, once, on the tip now. */
1935 async function recheckFinished(runId: string, repo: Repo): Promise<void> {
1936 // The common path, a red or a gated repository, reads no ref and no pull.
1937 if (landGated(repo)) return;
1938 const run = getRun(app.db, runId);
1939 const number = /^pull-(\d+)$/.exec(run?.env ?? "")?.[1];
1940 if (run === undefined || number === undefined || run.state !== "ok" || run.baseOid === "") return;
1941 const pull = getPull(app.db, repo.id, Number(number));
1942 if (!pull || pull.id !== run.issueId || pull.headOid !== run.commitOid) return;
1943 // A branch's tip is a file read (`resolveOid`), no process.
1944 const tip = await resolveOid(await pullDir(app.db, app.reposRoot, repo, pull), `refs/heads/${pull.baseBranch}`);
1945 if (tip === undefined || tip === run.baseOid) return;
1946 if (dueOf(pull, tip) !== undefined) await askInOrder(repo, [pull]);
1947 }
1948
18751949 async function headOf(repo: Repo, pull: Pull): Promise<string | undefined> {
18761950 const dir = await pullDir(app.db, app.reposRoot, repo, pull);
18771951 const ref = pull.crossRepo
test/check-base-moved.test.ts added +184
@@ -0,0 +1,184 @@
1 /**
2 * A pull's check is of its head merged into the base's tip; a conflict is a
3 * named failure, and a green is asked again by itself once the base moves,
4 * at most once per move (#12533).
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { openPull } from "../src/actions/pulls.ts";
12 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
13 import { BASE_MOVED, dueGreen, newestBaseFirst, recheckOnBaseMove } from "../src/checks/base_moved.ts";
14 import { startChecks, type ChecksHandle } from "../src/checks/service.ts";
15 import { checksForHead, listActive, listRuns, type Run } from "../src/core/runs.ts";
16 import { getPull } from "../src/core/pulls.ts";
17 import { createUser, type User } from "../src/core/users.ts";
18 import { getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts";
19 import { serve, type Servers } from "../src/http/server.ts";
20
21 const A = "a".repeat(40);
22 const B = "b".repeat(40);
23
24 const run = (over: Partial<Run>): Run =>
25 ({ kind: "check", env: "pull-1", state: "ok", baseOid: A, ...over }) as Run;
26
27 describe("newestBaseFirst", () => {
28 const due = (number: number, startedAt: string) => ({ number, green: run({ startedAt, createdAt: startedAt }) });
29 test("the green that started latest, of the newest base, goes first; ties to the lower number", () => {
30 const order = newestBaseFirst([due(1, "2026-11-14T10:00:00.000Z"), due(3, "2026-11-14T11:00:00.000Z"), due(2, "2026-11-14T11:00:00.000Z")]);
31 expect(order.map((d) => d.number)).toEqual([2, 3, 1]);
32 });
33 });
34
35 describe("recheckOnBaseMove", () => {
36 test("a green of an older base is asked again", () => {
37 expect(recheckOnBaseMove([run({})], "pull-1", B, false)).toBe(true);
38 });
39 test("a green of this base is not", () => {
40 expect(recheckOnBaseMove([run({ baseOid: B })], "pull-1", B, false)).toBe(false);
41 });
42 test("a check queued or running is never re-queued by a move", () => {
43 expect(recheckOnBaseMove([run({ state: "queued", baseOid: "" }), run({})], "pull-1", B, false)).toBe(false);
44 expect(recheckOnBaseMove([run({ state: "running" }), run({})], "pull-1", B, false)).toBe(false);
45 });
46 test("once per base: a head checked against this tip before is not checked again", () => {
47 expect(recheckOnBaseMove([run({}), run({ state: "cancelled", baseOid: B })], "pull-1", B, false)).toBe(false);
48 });
49 test("a red, a check that merged nothing, another environment or a gated land is left", () => {
50 expect(recheckOnBaseMove([run({ state: "failed" })], "pull-1", B, false)).toBe(false);
51 expect(recheckOnBaseMove([run({ baseOid: "" })], "pull-1", B, false)).toBe(false);
52 expect(recheckOnBaseMove([run({ env: "pull-2" })], "pull-1", B, false)).toBe(false);
53 expect(recheckOnBaseMove([run({})], "pull-1", B, true)).toBe(false);
54 expect(recheckOnBaseMove([], "pull-1", B, false)).toBe(false);
55 });
56 });
57
58 let dataDir: string;
59 let app: App;
60 let servers: Servers;
61 let checks: ChecksHandle;
62 let alice: User;
63 let repo: Repo;
64
65 const pullRuns = (env: string): Run[] =>
66 listRuns(app.db, repo.id, { kind: "check", limit: 100 }).filter((r) => r.env === env);
67
68 async function settled(env: string, count: number): Promise<Run[]> {
69 const deadline = Date.now() + 30_000;
70 while (Date.now() < deadline) {
71 const runs = pullRuns(env);
72 if (runs.length >= count && runs.every((r) => r.state !== "queued" && r.state !== "running")) return runs;
73 await Bun.sleep(50); // real-timer: the check is a real git merge and process in another task
74 }
75 throw new Error(`${env} did not settle`);
76 }
77
78 async function commit(branch: string, path: string, content: string): Promise<string> {
79 const made = await commitToBranch(app, alice, repo, { branch, message: `Write ${path}`, files: [{ path, content }] });
80 if (!made.ok) throw new Error(JSON.stringify(made.error));
81 return made.value.commitOid;
82 }
83
84 async function pull(branch: string, path: string, content: string): Promise<number> {
85 await commit(branch, path, content);
86 const opened = await openPull(app, alice, repo, { title: branch, baseBranch: "main", headBranch: branch });
87 if (!opened.ok) throw new Error(String(opened.error));
88 return opened.value.number;
89 }
90
91 beforeAll(async () => {
92 dataDir = await mkdtemp(join(tmpdir(), "forge-base-moved-"));
93 app = createApp({ dataDir, port: 0 });
94 servers = await serve(app);
95 checks = startChecks(app, { forgeUrl: servers.url, concurrency: 1 });
96 const a = await createUser(app.db, {
97 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
98 password: "a-good-passphrase", adminIfFirst: true,
99 });
100 if (!a.ok) throw new Error(a.error);
101 alice = a.value;
102 const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
103 if (!made.ok) throw new Error(String(made.error));
104 repo = made.value;
105 await commit("main", "forge.json", `{"check": {"run": "true", "install": ""}}\n`);
106 await commit("main", "shared.txt", "one\n");
107 await settled("main", 2);
108 });
109
110 afterAll(async () => {
111 await checks.stop();
112 servers.stop();
113 app.close();
114 await rm(dataDir, { recursive: true, force: true });
115 });
116
117 /**
118 * Nothing waits or runs for the pull, and its head is not due again on this tip: the queue is
119 * quiet.
120 */
121 function quiet(n: number, tip: string): void {
122 const env = `pull-${n}`;
123 expect(listActive(app.db, "check").filter((r) => r.repoId === repo.id && r.env === env)).toEqual([]);
124 const p = getPull(app.db, repo.id, n)!;
125 expect(dueGreen(checksForHead(app.db, p.id, p.headOid), env, tip, false)).toBeUndefined();
126 }
127
128 test("a clean merge passes, and a moved base checks the green again once, on the new tip", async () => {
129 const n = await pull("clean", "clean.txt", "clean\n");
130 const [first] = await settled(`pull-${n}`, 1);
131 expect(first!.state).toBe("ok");
132 expect(first!.baseOid).toMatch(/^[0-9a-f]{40}$/);
133
134 const tip = await commit("main", "other.txt", "moved\n");
135 const runs = await settled(`pull-${n}`, 2);
136 expect(runs.length).toBe(2);
137 const again = runs.find((r) => r.reason === BASE_MOVED)!;
138 expect(again.state).toBe("ok");
139 expect(again.baseOid).toBe(tip);
140 // Main's own check settling asks for nothing more: this head knows this tip.
141 await settled("main", 3);
142 quiet(n, tip);
143 }, 60_000);
144
145 test("a move's re-checks take the slot newest base first, not in pull order", async () => {
146 const older = await pull("older", "older.txt", "older\n");
147 const newer = await pull("newer", "newer.txt", "newer\n");
148 await settled(`pull-${newer}`, 1);
149 await settled(`pull-${older}`, 1);
150 // A push to the lower-numbered pull makes its green the one that started latest.
151 await commit("older", "older.txt", "older again\n");
152 await settled(`pull-${older}`, 2);
153 const tip = await commit("main", "order.txt", "moved\n");
154 const [a, b] = await Promise.all([settled(`pull-${older}`, 3), settled(`pull-${newer}`, 2)]);
155 const startOf = (runs: Run[]) => runs.find((r) => r.reason === BASE_MOVED && r.baseOid === tip)!.startedAt!;
156 // Asked in pull order, the queue's newest-request-first would start `newer` first.
157 expect(startOf(a) < startOf(b)).toBe(true);
158 }, 60_000);
159
160 test("a conflicted merge fails naming the files in conflict", async () => {
161 await commit("clash", "shared.txt", "theirs\n");
162 await commit("main", "shared.txt", "ours\n");
163 // Main moved before the pull opened: its first check is of the conflicted merge.
164 const opened = await openPull(app, alice, repo, { title: "clash", baseBranch: "main", headBranch: "clash" });
165 if (!opened.ok) throw new Error(String(opened.error));
166 const [first] = await settled(`pull-${opened.value.number}`, 1);
167 expect(first!.reason).not.toBe(BASE_MOVED);
168 expect(first!.state).toBe("failed");
169 expect(first!.summary).toContain("does not merge into main: conflicts in shared.txt");
170 }, 60_000);
171
172 test("where the merge queue gates lands, a moved base asks no re-check: the gate merges with the tip", async () => {
173 const n = await pull("gated", "gated.txt", "gated\n");
174 await settled(`pull-${n}`, 1);
175 repo = updateRepoSettings(app.db, getRepoById(app.db, repo.id)!, { autonomy: "autonomous", autonomyBy: alice.id });
176 const mains = pullRuns("main").length;
177 const tip = await commit("main", "gated-move.txt", "moved\n");
178 await settled("main", mains + 1);
179 expect(pullRuns(`pull-${n}`).length).toBe(1);
180 expect(listActive(app.db, "check").filter((r) => r.repoId === repo.id && r.env === `pull-${n}`)).toEqual([]);
181 const p = getPull(app.db, repo.id, n)!;
182 // Due by itself, so the skip is the gate's and nothing else's.
183 expect(dueGreen(checksForHead(app.db, p.id, p.headOid), `pull-${n}`, tip, false)).toBeDefined();
184 }, 60_000);
test/check-tiers.test.ts +1 −1
@@ -977,7 +977,7 @@ describe("a pull's fast check is of the merge", () => {
977977
978978 const clash = await settled(conflicting);
979979 expect(clash.state).toBe("failed");
980 expect(clash.summary).toContain("does not merge into main: shared.txt");
980 expect(clash.summary).toContain("does not merge into main: conflicts in shared.txt");
981981 });
982982
983983 test("a green default-branch run keeps the impact map, and the next fast check is seeded with it", async () => {
test/merged-check.test.ts +1 −1
@@ -121,7 +121,7 @@ describe("mergeForCheck: the pull as it would land on today's base", () => {
121121 expect(m.ok).toBe(false);
122122 if (m.ok) return;
123123 expect(m.error.startsWith(NO_MERGE)).toBe(true);
124 expect(m.error).toBe(`${NO_MERGE} main: shared.txt`);
124 expect(m.error).toBe(`${NO_MERGE} main: conflicts in shared.txt`);
125125 expect([await refs(pullDir), await refs(baseDir)]).toEqual(before);
126126 });
127127 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs