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
Check · Review · Land · Live done — Landed and live in production.
talk/fix-flaky-on-main → main 0 commits
4 files
changed +92
−30
scripts/test-shards.ts +23 −21
| @@ -123,7 +123,7 @@ import { FLAKY_ALONE, onlyFiles } from "../src/checks/verify.ts"; | ||
| 123 | 123 | import { crashFlakeLine } from "../src/checks/flake.ts"; |
| 124 | 124 | import { formatKept, keptLine, mergeKept, parseKept, type KeptEntry } from "../src/checks/kept.ts"; |
| 125 | 125 | 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"; | |
| 127 | 127 | |
| 128 | 128 | const TIMES = new URL("./test-times.json", import.meta.url).pathname; |
| 129 | 129 | // 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 | ||
| 1207 | 1207 | did.set(file, { did: "absent" }); |
| 1208 | 1208 | continue; |
| 1209 | 1209 | } |
| 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"; | |
| 1225 | 1229 | // 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)); | |
| 1231 | 1233 | } |
| 1232 | 1234 | } finally { |
| 1233 | 1235 | git(["worktree", "remove", "--force", tree]); |
src/checks/base_red.ts +33 −5
| @@ -10,7 +10,9 @@ | ||
| 10 | 10 | * the change's), a file the base does not have, one green on the base, a |
| 11 | 11 | * test that fails on the pull and passes on the base (a second break in a |
| 12 | 12 | * 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 | |
| 14 | 16 | * about each: main's own check runs no tests, so a main red in forty |
| 15 | 17 | * files was every pull's red, and each was parked for it (2026-11-13). |
| 16 | 18 | * |
| @@ -24,10 +26,30 @@ | ||
| 24 | 26 | * there to run. |
| 25 | 27 | */ |
| 26 | 28 | 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 } | |
| 29 | 31 | | { readonly did: "absent" }; |
| 30 | 32 | |
| 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 | ||
| 31 | 53 | /** Each red file of the pull's run, with the tests in it that failed. */ |
| 32 | 54 | export type RedTests = ReadonlyMap<string, readonly string[]>; |
| 33 | 55 | |
| @@ -75,13 +97,19 @@ export function baseVerdict(red: RedTests, onBase: ReadonlyMap<string, OnBase>, | ||
| 75 | 97 | for (const [file, failing] of red) { |
| 76 | 98 | const there = onBase.get(file); |
| 77 | 99 | 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`); | |
| 79 | 102 | else if (there.did === "absent") theirs.push(`${file} is not on ${at}: this pull's red`); |
| 80 | 103 | else { |
| 81 | 104 | const own = failing.filter((name) => !there.failing.includes(name)); |
| 82 | 105 | if (failing.length > 0 && own.length === 0) { |
| 83 | 106 | 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 | ); | |
| 85 | 113 | } else if (there.failing.length === 0) |
| 86 | 114 | theirs.push(`${file} is red on ${at}, but names no failing test there to compare: this pull's red`); |
| 87 | 115 | 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"; | ||
| 9 | 9 | import { tmpdir } from "node:os"; |
| 10 | 10 | import { join } from "node:path"; |
| 11 | 11 | 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"; | |
| 13 | 13 | |
| 14 | 14 | const BASE = "0123456789abcdef0123456789abcdef01234567"; |
| 15 | 15 | const red = (...failing: string[]) => ({ did: "red" as const, failing }); |
| @@ -28,6 +28,35 @@ describe("whose red it is", () => { | ||
| 28 | 28 | ]); |
| 29 | 29 | }); |
| 30 | 30 | |
| 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 | ||
| 31 | 60 | test("a second break in a file main already has red is the pull's", () => { |
| 32 | 61 | const got = baseVerdict( |
| 33 | 62 | new Map([["test/a.test.ts", ["x", "new break"]]]), |
test/tui-offers.test.ts +6 −3
| @@ -39,12 +39,15 @@ beforeAll(async () => { | ||
| 39 | 39 | dir = await mkdtemp(join(tmpdir(), "forge-tui-offers-")); |
| 40 | 40 | app = createApp({ dataDir: dir, port: 0, host: "forge.test", trustedProxies: LOOPBACK_PROXIES }); |
| 41 | 41 | 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> => { | |
| 43 | 45 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: username === "alice" }); |
| 44 | 46 | if (!r.ok) throw new Error(r.error); |
| 45 | 47 | return r.value; |
| 46 | }); | |
| 47 | [alice, bob] = [await people[0]!, await people[1]!]; | |
| 48 | }; | |
| 49 | alice = await person("alice"); | |
| 50 | bob = await person("bob"); | |
| 48 | 51 | const made = await createRepo(app, alice, { name: "lib", visibility: "public" }); |
| 49 | 52 | if (!made.ok) throw new Error(String(made.error)); |
| 50 | 53 | repo = made.value; |
new above
offline