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

Two reds that were not the pull's: two signups at once in tui-offers, and main asked only once #16281

Merged opened this

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

Merged

talk/fix-flaky-on-main → main 0 commits

  • Merged. talk/fix-flaky-on-main was merged into main as b7e66b9.

Review

Sign in to review this change.
4 files changed +92 −30
scripts/test-shards.ts +23 −21
@@ -123,7 +123,7 @@ import { FLAKY_ALONE, onlyFiles } from "../src/checks/verify.ts";
123123 import { crashFlakeLine } from "../src/checks/flake.ts";
124124 import { formatKept, keptLine, mergeKept, parseKept, type KeptEntry } from "../src/checks/kept.ts";
125125 import { answerKept, freshKept } from "./kept-input.ts";
126 import { askBase, baseVerdict, type OnBase } from "../src/checks/base_red.ts";
126 import { askBase, baseVerdict, untilRed, type OnBase } from "../src/checks/base_red.ts";
127127
128128 const TIMES = new URL("./test-times.json", import.meta.url).pathname;
129129 // The forge hands the map over in the checkout (`src/checks/impact-store.ts`):
@@ -1207,27 +1207,29 @@ async function runOnBase(asked: readonly string[], base: string): Promise<Map<st
12071207 did.set(file, { did: "absent" });
12081208 continue;
12091209 }
1210 mkdirSync(scratch.tmp, { recursive: true });
1211 mkdirSync(scratch.data, { recursive: true });
1212 const child = Bun.spawn(["bun", "test", ...flags, `./${file}`], {
1213 cwd: tree,
1214 env,
1215 stdin: "ignore",
1216 stdout: "pipe",
1217 stderr: "pipe",
1218 });
1219 children.add(child);
1220 const output: string[] = [];
1221 await Promise.all([drain(child.stdout, output), drain(child.stderr, output)]);
1222 const report = output.join("");
1223 if ((await child.exited) === 0) did.set(file, { did: "green" });
1224 else {
1210 // Run again while green, up to BASE_RUNS (`untilRed`): a flaky test on main is main's too.
1211 const once = async (): Promise<"green" | readonly string[]> => {
1212 mkdirSync(scratch.tmp, { recursive: true });
1213 mkdirSync(scratch.data, { recursive: true });
1214 const child = Bun.spawn(["bun", "test", ...flags, `./${file}`], {
1215 cwd: tree,
1216 env,
1217 stdin: "ignore",
1218 stdout: "pipe",
1219 stderr: "pipe",
1220 });
1221 children.add(child);
1222 const output: string[] = [];
1223 await Promise.all([drain(child.stdout, output), drain(child.stderr, output)]);
1224 const report = output.join("");
1225 const green = (await child.exited) === 0;
1226 children.delete(child);
1227 rmSync(scratch.data, { recursive: true, force: true });
1228 if (green) return "green";
12251229 // A crash or load error names no test: nothing to compare the pull's red with.
1226 const named = onlyTestFailures(report) ? failingTests(report) : new Map<string, string[]>();
1227 did.set(file, { did: "red", failing: [...named.values()].flat() });
1228 }
1229 children.delete(child);
1230 rmSync(scratch.data, { recursive: true, force: true });
1230 return onlyTestFailures(report) ? [...failingTests(report).values()].flat() : [];
1231 };
1232 did.set(file, await untilRed(once));
12311233 }
12321234 } finally {
12331235 git(["worktree", "remove", "--force", tree]);
src/checks/base_red.ts +33 −5
@@ -10,7 +10,9 @@
1010 * the change's), a file the base does not have, one green on the base, a
1111 * test that fails on the pull and passes on the base (a second break in a
1212 * file main already has red), and a red no file or test is named for (a
13 * crash, a type error). However many files are red, the base is asked
13 * crash, a type error). A file is run on the base up to `BASE_RUNS` times
14 * and is red there at its first red: red on a later run, it is flaky on
15 * main, and said so. However many files are red, the base is asked
1416 * about each: main's own check runs no tests, so a main red in forty
1517 * files was every pull's red, and each was parked for it (2026-11-13).
1618 *
@@ -24,10 +26,30 @@
2426 * there to run.
2527 */
2628 export type OnBase =
27 | { readonly did: "red"; readonly failing: readonly string[] }
28 | { readonly did: "green" }
29 | { readonly did: "red"; readonly failing: readonly string[]; readonly runs?: number }
30 | { readonly did: "green"; readonly runs?: number }
2931 | { readonly did: "absent" };
3032
33 /**
34 * How many times a red file is run on the base before it is called green there. A flaky test
35 * passes alone more often than not, so one green run on main blamed the pull for main's flake
36 * (#16213); the runs stop at the first red.
37 */
38 export const BASE_RUNS = 3;
39
40 /**
41 * One file on the base, run until its first red and at most `BASE_RUNS` times: `once` runs it and
42 * answers "green", or the tests that failed (none named when it crashed). Red is said with the run
43 * it came on; green with how many runs it took.
44 */
45 export async function untilRed(once: () => Promise<"green" | readonly string[]>): Promise<OnBase> {
46 for (let runs = 1; ; runs++) {
47 const did = await once();
48 if (did !== "green") return { did: "red", failing: did, runs };
49 if (runs >= BASE_RUNS) return { did: "green", runs };
50 }
51 }
52
3153 /** Each red file of the pull's run, with the tests in it that failed. */
3254 export type RedTests = ReadonlyMap<string, readonly string[]>;
3355
@@ -75,13 +97,19 @@ export function baseVerdict(red: RedTests, onBase: ReadonlyMap<string, OnBase>,
7597 for (const [file, failing] of red) {
7698 const there = onBase.get(file);
7799 if (there === undefined) theirs.push(`${file} did not run on ${at}: this pull's red`);
78 else if (there.did === "green") theirs.push(`${file} is green on ${at}: this pull's red`);
100 else if (there.did === "green")
101 theirs.push(`${file} is green on ${at}${(there.runs ?? 1) > 1 ? ` in ${there.runs} runs` : ""}: this pull's red`);
79102 else if (there.did === "absent") theirs.push(`${file} is not on ${at}: this pull's red`);
80103 else {
81104 const own = failing.filter((name) => !there.failing.includes(name));
82105 if (failing.length > 0 && own.length === 0) {
83106 mains.push(file);
84 lines.push(`${file} is red on ${at} too, the same tests: not counted against this pull`);
107 const runs = there.runs ?? 1;
108 lines.push(
109 runs > 1
110 ? `${file} is flaky on ${at}: green, then the same tests failed on run ${runs}: not counted against this pull`
111 : `${file} is red on ${at} too, the same tests: not counted against this pull`,
112 );
85113 } else if (there.failing.length === 0)
86114 theirs.push(`${file} is red on ${at}, but names no failing test there to compare: this pull's red`);
87115 else theirs.push(`${file} is red on ${at} too, but ${some(own)} pass${own.length === 1 ? "es" : ""} there: this pull's red`);
test/red-on-main.test.ts +30 −1
@@ -9,7 +9,7 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
99 import { tmpdir } from "node:os";
1010 import { join } from "node:path";
1111 import { EXCLUDED, SLOW } from "../scripts/gate.ts";
12 import { askBase, baseVerdict, clearedIn, type OnBase } from "../src/checks/base_red.ts";
12 import { askBase, BASE_RUNS, baseVerdict, clearedIn, untilRed, type OnBase } from "../src/checks/base_red.ts";
1313
1414 const BASE = "0123456789abcdef0123456789abcdef01234567";
1515 const red = (...failing: string[]) => ({ did: "red" as const, failing });
@@ -28,6 +28,35 @@ describe("whose red it is", () => {
2828 ]);
2929 });
3030
31 test("a file red on the base only on a later run is flaky there, and main's; red on the first run is red on main too", () => {
32 const verdict = (runs: number) =>
33 baseVerdict(new Map([["test/a.test.ts", ["x"]]]), new Map([["test/a.test.ts", { ...red("x"), runs }]]), BASE);
34 for (const runs of [2, 3]) {
35 expect(verdict(runs).cleared).toBe(true);
36 expect(verdict(runs).lines[0]).toBe(`test/a.test.ts is flaky on main at 0123456: green, then the same tests failed on run ${runs}: not counted against this pull`);
37 }
38 expect(verdict(1).lines[0]).toBe("test/a.test.ts is red on main at 0123456 too, the same tests: not counted against this pull");
39 const green = baseVerdict(new Map([["test/a.test.ts", ["x"]]]), new Map([["test/a.test.ts", { did: "green" as const, runs: 3 }]]), BASE);
40 expect(green.cleared).toBe(false);
41 expect(green.lines).toEqual(["test/a.test.ts is green on main at 0123456 in 3 runs: this pull's red"]);
42 });
43
44 test("the base runs a file until its first red, at most BASE_RUNS times, and says which run", async () => {
45 const runner = (answers: ("green" | string[])[]) => {
46 let calls = 0;
47 return { once: async () => answers[calls++] ?? "green", calls: () => calls };
48 };
49 const late = runner(["green", ["x"], "green"]);
50 expect(await untilRed(late.once)).toEqual({ did: "red", failing: ["x"], runs: 2 });
51 expect(late.calls()).toBe(2);
52 const first = runner([["x"]]);
53 expect(await untilRed(first.once)).toEqual({ did: "red", failing: ["x"], runs: 1 });
54 expect(first.calls()).toBe(1);
55 const never = runner([]);
56 expect(await untilRed(never.once)).toEqual({ did: "green", runs: BASE_RUNS });
57 expect(never.calls()).toBe(BASE_RUNS);
58 });
59
3160 test("a second break in a file main already has red is the pull's", () => {
3261 const got = baseVerdict(
3362 new Map([["test/a.test.ts", ["x", "new break"]]]),
test/tui-offers.test.ts +6 −3
@@ -39,12 +39,15 @@ beforeAll(async () => {
3939 dir = await mkdtemp(join(tmpdir(), "forge-tui-offers-"));
4040 app = createApp({ dataDir: dir, port: 0, host: "forge.test", trustedProxies: LOOPBACK_PROXIES });
4141 servers = await serve(app);
42 const people = ["alice", "bob"].map(async (username) => {
42 // One after the other: alice is the forge's admin only if her account is written first, and two
43 // signups at once race through the password hash, so bob's could land first (#16213).
44 const person = async (username: string): Promise<User> => {
4345 const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: username === "alice" });
4446 if (!r.ok) throw new Error(r.error);
4547 return r.value;
46 });
47 [alice, bob] = [await people[0]!, await people[1]!];
48 };
49 alice = await person("alice");
50 bob = await person("bob");
4851 const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
4952 if (!made.ok) throw new Error(String(made.error));
5053 repo = made.value;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs