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

Main's own revert path also waits for two reds on the same test, and both paths close their reverts on green #16831

Merged opened this

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

Merged

flow/revert-red-two → main 0 commits

  • Merged. flow/revert-red-two was merged into main as b35edd5.

Review

Sign in to review this change.
6 files changed +159 −30
src/checks/culprit.ts +23 −25
@@ -37,7 +37,7 @@ import { runGit } from "../git/env.ts";
3737 import { isSafeOid } from "../git/guard.ts";
3838 import { repoDir } from "../git/repos.ts";
3939 import { noRevertWhy, pinsLandedBy } from "./pinned_landing.ts";
40 import { everReverted, FORGE_CLOSED } from "./revert_red.ts";
40 import { everReverted, FORGE_CLOSED, REVERT_RED_SAYS } from "./revert_red.ts";
4141 import {
4242 BISECT_ENV,
4343 BISECT_PREFIX,
@@ -45,6 +45,7 @@ import {
4545 MAX_RANGE,
4646 maxSteps,
4747 nextStep,
48 sameFailure,
4849 VERIFY_ENV,
4950 } from "./verify.ts";
5051
@@ -93,18 +94,9 @@ function finishedAfter(app: App, repoId: string, above: string): Run | undefined
9394 return row === null ? undefined : getRun(app.db, row.id);
9495 }
9596
96 /**
97 * Whether red run `later` fails a test red run `red` failed: a name both name, or a file when
98 * either names none.
99 */
100 function sameTest(red: Run, later: Run): boolean {
101 if (later.state !== "failed") return false;
102 const names = red.checkDetail?.names ?? [];
103 const again = later.checkDetail?.names ?? [];
104 if (names.length > 0 && again.length > 0) return names.some((n) => again.includes(n));
105 const files = later.checkDetail?.files ?? [];
106 return (red.checkDetail?.files ?? []).some((f) => files.includes(f));
107 }
97 /** Whether red run `later` fails a test red run `red` failed (`sameFailure`). */
98 const sameTest = (red: Run, later: Run): boolean =>
99 later.state === "failed" && sameFailure(red.checkDetail ?? {}, later.checkDetail ?? {});
108100
109101 /** What each finished step of `origin`'s bisection said, by commit: green or not. */
110102 function stepsOf(app: App, repoId: string, origin: string): { green: Map<string, boolean>; vague: Run | undefined } {
@@ -365,16 +357,20 @@ export function closeOnGreen(app: App, repo: Repo, run: Run, passed: readonly st
365357 const REVERT_SAYS = /^Reverts #\d+: verifying main after landing found it broke /;
366358
367359 /**
368 * The lines for a green verification's log: each open revert this module proposed before `run` was
369 * asked for, whose every named file `run` printed passing while the pull it takes back is still on
370 * main, closed with that reason in one line and its branch dropped. The red it answered was not
371 * that pull's: main is green with it, and a later break of that pull may be reverted again
372 * (`everReverted` skips a revert the forge closed). A revert a person has commented on stays open:
373 * the comment is their say. The forge's own close comment is posted under the owner's name and says
374 * so; it is written as the revert closes, so it never stands as a person's word on an open one.
360 * The lines for a green run's log: each open revert the forge proposed before `run` was asked for —
361 * this module's after a verification, or `revert_red.ts`'s on main's own check — closed with the
362 * reason in one line and its branch dropped while the pull it takes back is still on main. The red
363 * it answered was not that pull's: main is green with it, and a later break of that pull may be
364 * reverted again (`everReverted` skips a revert the forge closed). `run` is a green verification,
365 * which must have printed passing every file a verification's revert names, or main's own green
366 * check, which passed all it runs. A revert a person has commented on stays open: the comment is
367 * their say. The forge's own close comment is posted under the owner's name and says so; it is
368 * written as the revert closes, so it never stands as a person's word on an open one.
375369 */
376370 export async function closeRevertsOnGreen(app: App, repo: Repo, run: Run, passed: readonly string[]): Promise<string[]> {
377 if (run.env !== VERIFY_ENV || run.state !== "ok" || passed.length === 0 || !isSafeOid(run.commitOid)) return [];
371 const verify = run.env === VERIFY_ENV && passed.length > 0;
372 const main = run.issueId === null && run.env === repo.defaultBranch;
373 if ((!verify && !main) || run.state !== "ok" || !isSafeOid(run.commitOid)) return [];
378374 const open = app.db
379375 .query<{ id: string; number: number; body: string; target: number; landed: string | null }, [string, string, string]>(
380376 `SELECT i.id, i.number, i.body, t.number AS target, tp.merged_oid AS landed
@@ -391,13 +387,15 @@ export async function closeRevertsOnGreen(app: App, repo: Repo, run: Run, passed
391387 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
392388 const lines: string[] = [];
393389 for (const r of open) {
394 if (owner === undefined || !REVERT_SAYS.test(r.body) || r.landed === null || !isSafeOid(r.landed)) continue;
395 const files = filesNamed(r.body);
396 if (files.length === 0 || !files.every((f) => passed.includes(f))) continue;
390 if (owner === undefined || r.landed === null || !isSafeOid(r.landed)) continue;
391 // A verification's revert names the files its red failed; main's own red names none.
392 const files = REVERT_SAYS.test(r.body) ? filesNamed(r.body) : REVERT_RED_SAYS.test(r.body) ? [] : undefined;
393 if (files === undefined || (REVERT_SAYS.test(r.body) && files.length === 0)) continue;
394 if (!main && !files.every((f) => passed.includes(f))) continue;
397395 if (!(await runGit(["merge-base", "--is-ancestor", r.landed, run.commitOid], { cwd: dir })).ok) continue;
398396 const issue = getIssueById(app.db, r.id);
399397 if (issue === undefined) continue;
400 const why = `${FORGE_CLOSED}verification ${run.id} is green at ${short(run.commitOid)} with #${r.target} still in it, so the red this revert answered was not #${r.target}'s. (The forge posts this under the owner's name; the owner did not write it.)`;
398 const why = `${FORGE_CLOSED}${main ? "check" : "verification"} ${run.id} is green at ${short(run.commitOid)} with #${r.target} still in it, so the red this revert answered was not #${r.target}'s. (The forge posts this under the owner's name; the owner did not write it.)`;
401399 commentAs(app, owner, repo, issue, why);
402400 if (!closeAs(app, owner, repo, issue, "closed").ok) continue;
403401 const pull = getPull(app.db, repo.id, r.number);
src/checks/execute.ts +5
@@ -230,6 +230,11 @@ export async function execute(
230230 proposeRevert(app, repo.id).catch((error) =>
231231 log.error("revert proposal failed", { error: String(error) }),
232232 );
233 // Main's own check green with a reverted landing still in it closes that revert (#6377).
234 if (done && outcome.ok && run.issueId === null && run.env === repo.defaultBranch)
235 await closeRevertsOnGreen(app, repo, done, [])
236 .then((lines) => lines.forEach(write))
237 .catch((error) => write(`reverts not closed: ${String(error)}`));
233238 // A red verification after landing names its culprit (#7786).
234239 if (done && run.env === VERIFY_ENV) {
235240 // Each red file is rerun alone on the same commit, once, before it is filed (#14404).
src/checks/revert_red.ts +30 −3
@@ -1,6 +1,7 @@
11 /**
2 * The way back from a red main (#6377). When main's check goes red and the
3 * commits since its last green are exactly one landed pull, the forge
2 * The way back from a red main (#6377). When main's check goes red twice in a
3 * row on the same failing test (`sameFailure`) and the commits from its last
4 * green to the first of those reds are exactly one landed pull, the forge
45 * proposes the revert of that pull: one pull, `revertsPullId` set, titled
56 * for the pull it takes back and naming the failing test. It is only ever
67 * proposed — a person lands it. A red that spans more than one landing (or a
@@ -14,12 +15,14 @@ import { revertPull } from "../actions/revert.ts";
1415 import { addComment, getIssueById } from "../core/issues.ts";
1516 import { getPull, type Pull } from "../core/pulls.ts";
1617 import { getRepoById } from "../core/repos.ts";
18 import { getRun, type Run } from "../core/runs.ts";
1719 import { getUserById } from "../core/users.ts";
1820 import { runGit } from "../git/env.ts";
1921 import { repoDir } from "../git/repos.ts";
2022 import { isSafeOid } from "../git/guard.ts";
2123 import { pinsLandedBy } from "./pinned_landing.ts";
2224 import { manifestError, ranOutOfTime, redMain, type RedMain } from "./red_main.ts";
25 import { sameFailure } from "./verify.ts";
2326
2427 /** The reds being answered now: a check finishing twice at once proposes once. */
2528 const working = new Set<string>();
@@ -36,6 +39,19 @@ function greenBefore(app: App, repoId: string, branch: string, below: string): s
3639 .get(repoId, branch, below)?.commit_oid;
3740 }
3841
42 /** The finished default-branch check just before run `below`, green or red. */
43 function finishedBefore(app: App, repoId: string, branch: string, below: string): Run | undefined {
44 const row = app.db
45 .query<{ id: string }, [string, string, string]>(
46 `SELECT id FROM run
47 WHERE repo_id = ?1 AND kind = 'check' AND issue_id IS NULL AND env = ?2
48 AND state IN ('ok','failed') AND id < ?3
49 ORDER BY id DESC LIMIT 1`,
50 )
51 .get(repoId, branch, below);
52 return row === null ? undefined : getRun(app.db, row.id);
53 }
54
3955 /**
4056 * How the forge's close of a revert it proposed opens, when a green showed the red was not its
4157 * target's (`culprit.ts`).
@@ -86,6 +102,11 @@ async function soleLanding(app: App, red: RedMain, green: string): Promise<Pull
86102 return getPull(app.db, repo.id, landed[0].number);
87103 }
88104
105 /**
106 * How a revert this module proposed opens: what `closeRevertsOnGreen` (`culprit.ts`) knows it by.
107 */
108 export const REVERT_RED_SAYS = /^Reverts #\d+, the one pull landed since main was last green\./;
109
89110 /** What the revert pull says besides what it reverts: the red it answers. */
90111 export function revertBody(pull: Pick<Pull, "number">, red: RedMain): string {
91112 const what =
@@ -137,7 +158,13 @@ export async function proposeRevert(app: App, repoId: string): Promise<Pull | un
137158 if (red === undefined || red.limit || manifestError(red) !== undefined) return undefined;
138159 const green = greenBefore(app, repoId, red.branch, red.runId);
139160 if (green === undefined) return undefined;
140 const landed = await soleLanding(app, red, green);
161 // Two reds in a row, failing the same test, before a revert (`sameFailure`, the rule
162 // `culprit.ts` holds too). The first red names the landing: the one since the green before it.
163 const first = finishedBefore(app, repoId, red.branch, red.runId);
164 const now = getRun(app.db, red.runId);
165 if (first === undefined || first.state !== "failed" || now === undefined) return undefined;
166 if (!sameFailure(first.checkDetail ?? {}, now.checkDetail ?? {})) return undefined;
167 const landed = await soleLanding(app, { ...red, commitOid: first.commitOid }, green);
141168 if (landed === undefined || everReverted(app, landed.id)) return undefined;
142169 const repo = getRepoById(app.db, repoId);
143170 // A revert that deletes a migration pin is red before it runs: the alarm's, not a pull's.
src/checks/verify.ts +18
@@ -172,3 +172,21 @@ export function onlyFiles(value: string | undefined): string[] | undefined {
172172 .filter((file) => file !== "" && safeFile(file))
173173 .slice(0, MAX_FILES);
174174 }
175
176 /**
177 * Whether red `later` fails a test red `red` failed: a name both name, or a file when either names
178 * none. A revert of a landing is proposed only when two red checks in a row fail the same test
179 * (`culprit.ts` after a verification, `revert_red.ts` on main's own check): one red was wrong 41
180 * times in 43, main green again later with the landing still in it. A red that names no test
181 * confirms nothing.
182 */
183 export function sameFailure(
184 red: { readonly names?: readonly string[]; readonly files?: readonly string[] },
185 later: { readonly names?: readonly string[]; readonly files?: readonly string[] },
186 ): boolean {
187 const names = red.names ?? [];
188 const again = later.names ?? [];
189 if (names.length > 0 && again.length > 0) return names.some((n) => again.includes(n));
190 const files = later.files ?? [];
191 return (red.files ?? []).some((f) => files.includes(f));
192 }
test/revert-red.test.ts +65 −2
@@ -10,11 +10,12 @@ import { join } from "node:path";
1010 import { createApp, type App } from "../src/app.ts";
1111 import { mergePull, openPull } from "../src/actions/pulls.ts";
1212 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
13 import { closeRevertsOnGreen } from "../src/checks/culprit.ts";
1314 import { proposeRevert } from "../src/checks/revert_red.ts";
1415 import { createIssue, listComments } from "../src/core/issues.ts";
1516 import { getPull, listPulls, type Pull } from "../src/core/pulls.ts";
1617 import type { Repo } from "../src/core/repos.ts";
17 import { createRun } from "../src/core/runs.ts";
18 import { createRun, getRun, type Run } from "../src/core/runs.ts";
1819 import { createUser, type User } from "../src/core/users.ts";
1920 import { resolveOid } from "../src/git/merge.ts";
2021 import { repoDir } from "../src/git/repos.ts";
@@ -57,6 +58,10 @@ describe("a red main after one landing proposes its revert", () => {
5758 );
5859 };
5960
61 /** The newest main check, as the checks service hands it over finished. */
62 const lastMain = (): Run =>
63 getRun(app.db, app.db.query<{ id: string }, [string]>("SELECT id FROM run WHERE repo_id = ?1 AND env = 'main' ORDER BY id DESC LIMIT 1").get(repo.id)!.id)!;
64
6065 const reverts = (): Pull[] =>
6166 listPulls(app.db, repo.id, { state: "all" }).filter((p) => p.revertsPullId !== null);
6267
@@ -88,10 +93,13 @@ describe("a red main after one landing proposes its revert", () => {
8893 await rm(dataDir, { recursive: true, force: true });
8994 });
9095
91 test("one landing then red opens one revert; a second tick opens none", async () => {
96 test("one landing then red twice on the same test opens one revert; one red alone opens none", async () => {
9297 await mainCheck("ok");
9398 const landed = await land("one", "beta\n");
9499 await mainCheck("failed", ["parses the thing"]);
100 // One red is not enough: most went green again with the landing still in (41 of 43).
101 expect(await proposeRevert(app, repo.id)).toBeUndefined();
102 await mainCheck("failed", ["parses the thing"]);
95103
96104 const first = await proposeRevert(app, repo.id);
97105 expect(first?.revertsPullId).toBe(landed.id);
@@ -109,6 +117,7 @@ describe("a red main after one landing proposes its revert", () => {
109117 await land("one", "beta\n");
110118 await land("two", "gamma\n");
111119 await mainCheck("failed", ["parses the thing"]);
120 await mainCheck("failed", ["parses the thing"]);
112121
113122 expect(await proposeRevert(app, repo.id)).toBeUndefined();
114123 expect(reverts()).toHaveLength(0);
@@ -152,17 +161,71 @@ describe("a red main after one landing proposes its revert", () => {
152161 await mainCheck("ok");
153162 await land("bad", "bad\n");
154163 await mainCheck("failed", ["parses the thing"], "check failed: 1 test failed");
164 await mainCheck("failed", ["parses the thing"], "check failed: 1 test failed");
155165 expect(await proposeRevert(app, repo.id)).toBeDefined();
156166 });
157167
168 test("two reds on different tests, or a red then a green, open none", async () => {
169 await mainCheck("ok");
170 await land("one", "beta\n");
171 await mainCheck("failed", ["parses the thing"]);
172 await mainCheck("failed", ["renders the page"]);
173 expect(await proposeRevert(app, repo.id)).toBeUndefined();
174 await mainCheck("ok");
175 await land("two", "gamma\n");
176 await mainCheck("failed", ["parses the thing"]);
177 await mainCheck("ok");
178 expect(await proposeRevert(app, repo.id)).toBeUndefined();
179 expect(reverts()).toHaveLength(0);
180 });
181
182 test("a landing between the two reds: the first red's one landing is the one reverted", async () => {
183 await mainCheck("ok");
184 const culprit = await land("one", "beta\n");
185 await mainCheck("failed", ["parses the thing"]);
186 // Another file, so the first landing's revert still applies on top of it.
187 await land("two", "beta\n", [{ path: "other.txt", content: "x\n" }]);
188 await mainCheck("failed", ["parses the thing"]);
189 expect((await proposeRevert(app, repo.id))?.revertsPullId).toBe(culprit.id);
190 });
191
158192 test("a landing that added a migration pin opens none: its revert would delete the pin (#16637)", async () => {
159193 await mainCheck("ok");
160194 await land("one", "beta\n", [{ path: "src/core/migration-pins/20261128100000-walks.pin", content: "abc\n" }]);
161195 await mainCheck("failed", ["parses the thing"]);
196 await mainCheck("failed", ["parses the thing"]);
162197 expect(await proposeRevert(app, repo.id)).toBeUndefined();
163198 expect(reverts()).toHaveLength(0);
164199 });
165200
201 test("main green again with the landing still in it closes the revert, saying why; a person's word keeps one", async () => {
202 await mainCheck("ok");
203 const landed = await land("one", "beta\n");
204 await mainCheck("failed", ["parses the thing"]);
205 await mainCheck("failed", ["parses the thing"]);
206 const revert = await proposeRevert(app, repo.id);
207 expect(revert).toBeDefined();
208 await mainCheck("ok");
209 expect(await closeRevertsOnGreen(app, repo, lastMain(), [])).toEqual([
210 `culprit: closed revert #${revert!.number}, #${landed.number} is green on main`,
211 ]);
212 expect(reverts()[0]!.state).toBe("closed");
213 const said = listComments(app.db, revert!.id).map((c) => c.body);
214 expect(said).toEqual([expect.stringContaining(`main's check ${lastMain().id} is green`)]);
215
216 // Another revert, a person's comment on it: it stays.
217 await mainCheck("ok");
218 const two = await land("two", "beta\n", [{ path: "two.txt", content: "2\n" }]);
219 await mainCheck("failed", ["parses the thing"]);
220 await mainCheck("failed", ["parses the thing"]);
221 const kept = await proposeRevert(app, repo.id);
222 expect(kept?.revertsPullId).toBe(two.id);
223 app.db.query("INSERT INTO comment (id, issue_id, author_id, body) VALUES ('c-person', ?1, ?2, 'keep this')").run(kept!.id, alice.id);
224 await mainCheck("ok");
225 expect(await closeRevertsOnGreen(app, repo, lastMain(), [])).toEqual([]);
226 expect(getPull(app.db, repo.id, kept!.number)?.state).toBe("open");
227 });
228
166229 test("a green main opens none", async () => {
167230 await mainCheck("ok");
168231 await land("one", "beta\n");
test/verify-after-landing.test.ts +18
@@ -326,6 +326,24 @@ describe("a red verification names its culprit", () => {
326326 expect(everReverted(app, one.id)).toBe(true);
327327 });
328328
329 test("main's own green closes a verification's revert, and the same break twice more proposes it again", async () => {
330 await verification("ok");
331 const one = await land("one");
332 await afterVerify(app, repo, await verification("failed"), async () => undefined);
333 await afterVerify(app, repo, await verification("failed"), async () => undefined);
334 const [first] = reverts();
335 expect(first?.revertsPullId).toBe(one.id);
336 // Main's own check is green on the tip with #one in it: the revert closes.
337 const green = createRun(app.db, { repoId: repo.id, kind: "check", env: "main", commitOid: await tip() });
338 expect(await closeRevertsOnGreen(app, repo, finish(green, "ok"), [])).toEqual([`culprit: closed revert #${first!.number}, #${one.number} is green on main`]);
339 // The verification is red again on the same test, the red before it beside it, still since the
340 // same green: proposed again, once.
341 const again = await afterVerify(app, repo, await verification("failed"), async () => undefined);
342 expect(again.at(-1)).toContain("proposed its revert");
343 expect((await afterVerify(app, repo, await verification("failed"), async () => undefined)).at(-1)).toContain("already proposed");
344 expect(reverts().filter((p) => p.state === "open").map((p) => p.revertsPullId)).toEqual([one.id]);
345 });
346
329347 test("a revert a person commented on stays open on green", async () => {
330348 await verification("ok");
331349 await land("one");

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs