Public: anyone can read this; the crew can change it.
A flaky test refiled after its issue closed names the earlier closes and goes first, not a new copy for the twentieth time #15370
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15365-a-flaky-test-refiled-after-its-issue-clo → main 0 commits fork 2 threads · 2 unresolved
3 files
changed +135
−4
docs/features/check-flake.md +1 −1
| @@ -14,7 +14,7 @@ cell, before main counts as red (`src/checks/flake.ts`, wired in | ||
| 14 | 14 | `flaky: … recorded, main stays green`. |
| 15 | 15 | - One fails alone too, or the red was a build error, a hook, a timeout or more |
| 16 | 16 | than two tests: **red, unchanged**. |
| 17 | - A test that flakes **twice in 7 days** gets one issue (`Flaky test: <name>`, | |
| 17 | - A test that flakes **twice in 7 days** gets one issue (`Flaky test: <name>`; with two or more closed issues under that title the new one lists them and carries `repeat-flake`, and with three says a bound or retry does not close it, | |
| 18 | 18 | `Files:` its file). An open issue by that title is not filed again. |
| 19 | 19 | - The week's list is `flakes` in the vitals (the health answer for an admin): |
| 20 | 20 | `repo`, `test`, `file`, `count`, `lastCommit`, `last`. A site admin reads it as |
src/checks/flake.ts +45 −3
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | import type { Database } from "bun:sqlite"; |
| 13 | import { createIssue, setIssueLabels } from "../core/issues.ts"; | |
| 13 | import { createIssue, createLabel, getLabel, setIssueLabels } from "../core/issues.ts"; | |
| 14 | 14 | import type { Repo } from "../core/repos.ts"; |
| 15 | 15 | import type { EventBus } from "../core/events.ts"; |
| 16 | 16 | import { id } from "../kernel/id.ts"; |
| @@ -229,6 +229,41 @@ export function recordCrashFlakes( | ||
| 229 | 229 | return crashes.length; |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | /** | |
| 233 | * A flake with this many closed issues under its title is a repeat: a root-cause task, not a retry. | |
| 234 | */ | |
| 235 | export const REPEAT_AT = 2; | |
| 236 | /** With this many, a bound or a retry does not close it. */ | |
| 237 | export const NO_QUARANTINE_AT = 3; | |
| 238 | export const REPEAT_LABEL = "repeat-flake"; | |
| 239 | const CLOSED_LISTED = 20; | |
| 240 | ||
| 241 | /** The numbers of the closed issues filed under `title`, oldest first. */ | |
| 242 | export function closedPredecessors(db: Database, repoId: string, title: string): number[] { | |
| 243 | return db | |
| 244 | .query<{ number: number }, [string, string]>( | |
| 245 | "SELECT number FROM issue WHERE repo_id = ?1 AND title = ?2 AND state = 'closed' ORDER BY number LIMIT 20", | |
| 246 | ) | |
| 247 | .all(repoId, title) | |
LIMIT 20 is a literal that duplicates CLOSED_LISTED, and it keeps the oldest 20, so closed.length understates the count past 20 and drops the newest closes. Count separately and list the newest. |
||
| 248 | .map((r) => r.number); | |
| 249 | } | |
| 250 | ||
| 251 | function repeatLabel(db: Database, repoId: string): string | undefined { | |
| 252 | const made = createLabel(db, repoId, REPEAT_LABEL, "b60205"); | |
| 253 | return made.ok ? made.value.id : getLabel(db, repoId, REPEAT_LABEL)?.id; | |
| 254 | } | |
| 255 | ||
| 256 | /** The body's paragraph for a repeat: the earlier closes, and the rule once there are three. */ | |
| 257 | export function repeatNote(closed: readonly number[]): string { | |
| 258 | if (closed.length < REPEAT_AT) return ""; | |
| 259 | const list = closed.slice(0, CLOSED_LISTED).map((n) => `#${n}`).join(", "); | |
| 260 | const rule = | |
| 261 | closed.length >= NO_QUARANTINE_AT | |
| 262 | ? ` A pull that only changes a bound or adds a retry does not close this: fix the cause; quarantine is not allowed.` | |
| 263 | : ""; | |
| 264 | return `\n\n**Repeat:** this flake was filed and closed ${closed.length} times before (${list}). Treat it as a root-cause task, not a retry.${rule}`; | |
| 265 | } | |
| 266 | ||
| 232 | 267 | export const flakeTitle = (name: string): string => `Flaky test: ${name.slice(0, 200)}`; |
| 233 | 268 | |
| 234 | 269 | /** |
| @@ -258,13 +293,20 @@ export function recordFlakes( | ||
| 258 | 293 | .get(repo.id, t.name, since)!.n; |
| 259 | 294 | // No issue without a file: the "Files:" line is what routes it to a builder. |
| 260 | 295 | if (count < FILE_AT || t.file === "") continue; |
| 296 | const title = flakeTitle(t.name); | |
| 297 | const closed = closedPredecessors(db, repo.id, title); | |
| 298 | const repeat = closed.length >= REPEAT_AT; | |
| 299 | const label = repeat ? repeatLabel(db, repo.id) : undefined; | |
| 261 | 300 | const filed = fileIssueOnce(db, bus, repo, { |
| 262 | title: flakeTitle(t.name), | |
| 301 | title, | |
| 263 | 302 | body: |
| 264 | 303 | `_Filed by the check runner from a check log, not written by a person._\n\n` + |
| 265 | 304 | `\`${t.name}\` in \`${t.file}\` failed in ${where} and passed when run alone, ${count} times in 7 days ` + |
| 266 | 305 | `(latest: check run ${ref.runId}, ${ref.commit.slice(0, 7)}). The check stayed green each time. ` + |
| 267 | `Find what it depends on: time, load, order, or shared state.\n\nFiles: ${t.file}`, | |
| 306 | `Find what it depends on: time, load, order, or shared state.` + | |
| 307 | repeatNote(closed) + | |
| 308 | `\n\nFiles: ${t.file}`, | |
| 309 | ...(label === undefined ? {} : { labelId: label }), | |
| 268 | 310 | }); |
| 269 | 311 | if (filed.opened) opened.push(filed.number); |
| 270 | 312 | } |
test/flake-repeat.test.ts added +89
| @@ -0,0 +1,89 @@ | ||
| 1 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createRepo } from "../src/actions/repos.ts"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { recordFlakes, REPEAT_LABEL } from "../src/checks/flake.ts"; | |
| 8 | import { setIssueState } from "../src/core/issues.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | ||
| 12 | quietLogs(); | |
| 13 | ||
| 14 | let dir: string; | |
| 15 | let app: App; | |
| 16 | beforeAll(async () => { | |
| 17 | dir = await mkdtemp(join(tmpdir(), "flake-repeat-")); | |
| 18 | app = createApp({ dataDir: dir, port: 0, host: "forge.test" }); | |
| 19 | }); | |
| 20 | afterAll(async () => { | |
| 21 | await rm(dir, { recursive: true, force: true }); | |
| 22 | }); | |
| 23 | ||
| 24 | let alice: Awaited<ReturnType<typeof createUser>> | undefined; | |
| 25 | async function ensureOwner() { | |
| 26 | alice ??= await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 27 | if (!alice.ok) throw new Error(alice.error); | |
| 28 | return alice.value; | |
| 29 | } | |
| 30 | ||
| 31 | async function setup(name: string) { | |
| 32 | const owner = await ensureOwner(); | |
| 33 | const made = await createRepo(app, owner, { name, visibility: "public" }); | |
| 34 | if (!made.ok) throw new Error("repo"); | |
| 35 | const repo = made.value; | |
| 36 | const t = [{ file: "t/a.test.ts", name: `slow > ${name}` }]; | |
| 37 | let n = 0; | |
| 38 | const file = () => recordFlakes(app.db, app.bus, repo, t, { runId: `r${++n}`, commit: `c${n}aaaaaa` }); | |
| 39 | const rows = () => | |
| 40 | app.db | |
| 41 | .query<{ id: string; number: number; state: string; body: string }, [string]>("SELECT id, number, state, body FROM issue WHERE repo_id = ?1 ORDER BY number") | |
| 42 | .all(repo.id); | |
| 43 | const labels = (issueId: string) => | |
| 44 | app.db | |
| 45 | .query<{ name: string }, [string]>("SELECT label.name FROM issue_label JOIN label ON label.id = issue_label.label_id WHERE issue_id = ?1") | |
| 46 | .all(issueId) | |
| 47 | .map((l) => l.name); | |
| 48 | const closeOpen = () => setIssueState(app.db, rows().find((r) => r.state === "open")!.id, "closed"); | |
| 49 | return { file, rows, labels, closeOpen }; | |
| 50 | } | |
| 51 | ||
| 52 | test("five filings of one title over closes give one open issue naming the closed four", async () => { | |
| 53 | const { file, rows, labels, closeOpen } = await setup("five"); | |
| 54 | file(); | |
| 55 | file(); // the second flake files the first issue | |
| 56 | for (let i = 0; i < 3; i++) { | |
| 57 | closeOpen(); | |
| 58 | file(); | |
| 59 | } | |
| 60 | closeOpen(); | |
| 61 | file(); | |
| 62 | const all = rows(); | |
| 63 | const open = all.filter((r) => r.state === "open"); | |
| 64 | expect(open).toHaveLength(1); | |
| 65 | const closed = all.filter((r) => r.state === "closed").map((r) => `#${r.number}`); | |
| 66 | expect(closed).toHaveLength(4); | |
| 67 | for (const c of closed) expect(open[0]!.body).toContain(c); | |
| 68 | expect(open[0]!.body).toContain("quarantine is not allowed"); | |
| 69 | expect(labels(open[0]!.id)).toContain(REPEAT_LABEL); | |
| 70 | expect(all[0]!.body).not.toContain("Repeat"); | |
| 71 | expect(all[0]!.body).not.toContain("quarantine"); | |
| 72 | }); | |
| 73 | ||
| 74 | test("two closed predecessors mark a repeat but do not yet forbid a bound; one is plain", async () => { | |
| 75 | const { file, rows, labels, closeOpen } = await setup("two"); | |
| 76 | file(); | |
| 77 | file(); | |
| 78 | closeOpen(); | |
| 79 | file(); | |
| 80 | const one = rows().find((r) => r.state === "open")!; | |
| 81 | expect(one.body).not.toContain("Repeat"); | |
| 82 | expect(labels(one.id)).toEqual([]); | |
| 83 | closeOpen(); | |
| 84 | file(); | |
| 85 | const two = rows().find((r) => r.state === "open")!; | |
| 86 | expect(two.body).toContain("**Repeat:**"); | |
| 87 | expect(two.body).not.toContain("quarantine"); | |
| 88 | expect(labels(two.id)).toEqual([REPEAT_LABEL]); | |
| 89 | }); |
new above
offline
This line is far past the wrap width and it breaks the bullet. Give the repeat behaviour its own bullet.