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

No flaky tests: ten green runs to join the gate, a nightly flake hunt in random order, mechanical quarantine #1323

Closed opened this in answer to
Closed without merging

Flaky tests now have a ten-run admission bar, a nightly hunt that runs in random order, and quarantine through an issue instead of by hand. This redoes #1268.

Notes

stopped closed without shipping deploy 9830f23

opened this

Flaky tests now have a ten-run admission bar, a nightly hunt that runs in random order, and quarantine through an issue instead of by hand. This redoes #1268. That pull's red check came from its branch being cut before #1200: main's --tier fast command ran against an old scripts/test-shards.ts that didn't know the flag. Its hunt also had a real bug. Outside a terminal, bun test prints only (fail) lines and never (pass), so it could never see a test pass and would have called every flake "broken". Here the verdict is each file's exit code, which is also what the gate judges and what quarantine moves. Failing test names and the text above them go into the issue.

  • Admission: bun scripts/flake-hunt.ts admit test/x.test.ts runs the file ten times. Each run is its own bun test --randomize with a fresh seed, next to busy loops on half the cores. It prints the seeds as a record for the pull and exits non-zero unless all ten are green. test/flake.test.ts joins GATE in this pull, with its record:
    admission: test/flake.test.ts — 10/10 green, random order, under load
      seeds 1160696049 2617202673 3220893724 1474944788 398948630
            4182219366 4065044398 996205493 810529936 3770001322 — all green
  • Hunt: flake-hunt.ts hunt [--shard N/M] [--file-issues] runs every gate file five times. Each run gets a fresh seed that sets the file order (a seeded shuffle) and, through --randomize, the test order inside each file. It runs under the same load as admission and does not stop at the first failure. A file red in some runs and green in others is flaky. A file red in every run is reported as broken, not flaky. With --file-issues it files Flaky: test/x.test.ts carrying the failure text, the seed with a command to replay it, and the run's link. It skips files that already have an open issue.
  • Quarantine: flake-hunt.ts quarantine reads the open Flaky: issues and moves each file still in GATE to EXCLUDED under that issue's number. It edits scripts/gate.ts, so the issue is the record and nobody edits the list by hand.
  • Nightly: .forgejo/workflows/flake-hunt.yml runs at 02:17 UTC, split across 20 jobs because the runner cuts a job at about 30 minutes. A final job runs quarantine and opens a flake-hunt/<date> pull on the forge.
  • Root causes: the issue body lists shared port, real clock, order dependence, leaked server, and a timeout near the real duration, as tickboxes with the fix for each. It says a retry is not a fix.
  • No retry: test/flake.test.ts fails on --retry, --rerun-each, a bunfig retry = or a { retry: N }. It checks forge.json, bunfig.toml, package.json, the workflows, the gate scripts and every file in GATE.

What to look at: the file-level verdict in scripts/flake.ts (readRun and findFlakes), and quarantine(). The test loads the edited gate.ts and checks it with auditGate.

Not done:

  • The workflow has never run. It needs a FORGE_TOKEN secret and a FORGE_URL variable on the mirror.
  • No existing GATE file was put through admission, and the known flaky ones (apps-ws, apps-idle, signin-throttle, …) are not fixed. The first hunt will quarantine whichever of them flake.
  • The crew still re-runs a check once when the decider blames the environment (src/crew/dispatch.ts, retriedChecks). That is a retry at the check level, not inside the gate. Removing it changes how the crew behaves and belongs in its own issue.

Tests and checks:

  • tsc is clean.
  • flake, check-list, shards and hollow pass.
  • studio and reserved-names fail locally with the same unix-socket EPERM from the sandbox, on main as well as this branch, so that failure is not caused by this change.
  • A two-run hunt on one slice (3 files) ran clean.
  • quarantine against the live API listed the open issues and moved nothing.

To try it:

  • bun test test/flake.test.ts
  • bun scripts/flake-hunt.ts admit test/qr.test.ts
  • bun scripts/flake-hunt.ts hunt --runs 2 --shard 58/60

🤖 Generated with Claude Code

Closes #1243

Closes #1243

3 earlier comments
commented

The flaky 50 ms check in test/spawn-first-boot.test.ts is fixed. The suite now refuses any test that limits a measured time to under 200 ms, and the nightly flake hunt names this as a sixth root cause. Both are pushed to this branch.

What changed

  • The flaky test: "the child answers /healthz and / quickly once booted" now requires the median of five loopback fetches to be under 500 ms instead of 50 ms. It went red at 65.4 ms on the shared runner (trevato-apps/forge PR #36, shard 47). What it guards against is a boot on every request, which takes seconds, so 500 ms still catches that. This is the same bound used upstream.
  • A new root cause: scripts/flake.ts adds "a tight wall-clock bound" to ROOT_CAUSES. The fix it gives is to compare against a baseline measured in the same run, or to set the bound at WALL_CLOCK_FLOOR_MS (200) or more. Every issue the hunt files now lists it.
  • A new guard: tightBounds() sits next to retries(). It flags expect(<measured time>).toBeLessThan(N) (or …OrEqual) when N is a number under 200. "Measured time" means the expression mentions now, elapsed, took, ms, duration, latency, times, started or began. test/flake.test.ts runs it over every test/*.test.ts, gated or not, so an excluded file can't bring the pattern back when it rejoins. Counts, sizes and status codes are left alone, and its examples check this.
  • Docs: README and CLAUDE.md now list the sixth root cause and the 200 ms floor.

What I chose not to do

  • I didn't rewrite the test as a relative assertion. Its point is "not a boot per request", and a generous fixed bound says exactly that.
  • I didn't change bounds of 1 s and up elsewhere, such as decide.test.ts and snapshot-store.test.ts. They are above the floor, and if one flakes the hunt will name it.
  • The detector only reads numbers written in the assertion. A bound stored in a named constant, like search.test.ts's LIVE_BUDGET_MS + 1_500, isn't checked. A full-suite scan turned up no other bound under 200 ms.

How to try it

  • bun test test/flake.test.ts passes. With the old 50 ms line restored, it fails and names test/spawn-first-boot.test.ts:245.
  • The type check is clean.
  • The edited test itself could not run here: the sandbox blocks the unix socket the spawned child listens on (EPERM on listen), so the forge's check is the first real run of it.

🤖 Generated with Claude Code

commented

The gate now hunts flaky test files instead of trusting them. To join the gate, a file has to pass ten runs in a row. A flaky file is quarantined against an issue number instead of being retried, and the hunt runs every night. The README explains how a file joins the gate and how it leaves.

What changed

  • scripts/gate.ts: adds test/flake.test.ts to the gate list, and holds the hunt, the quarantine-by-issue list and the ten-run admission rule.
  • Wall-clock bounds: any test file that asserts a timing bound under 200 ms is now refused, because tighter bounds fail on a busy machine for reasons unrelated to the code under test.
  • spawn-first-boot: its loopback median bound goes from 50 ms to 500 ms so it passes that rule. The old bound was too tight to hold on a loaded runner, so this is a test that was wrong, not a code change.
  • Merge with main: this branch and main had both added entries at the end of the GATE list. The merge keeps both, so flake.test.ts sits beside main's newly admitted ssh-site, web-code and web-origins tests. No file was dropped and none appears twice.

What to look at The exclusion list in scripts/gate.ts and the 200 ms rule are where the policy lives. The type check is clean and bun test test/flake.test.ts passes (17 tests).

What I chose not to do No test files were quarantined or rewritten beyond spawn-first-boot. A file that starts failing should get its own issue and go into quarantine by that issue number.

How to try it Run bun test test/flake.test.ts, or run the hunt script against one file and watch it ask for ten green runs before admitting it.

🤖 Generated with Claude Code

commented

A file can now join the test gate only with proof of ten green runs in random order under load. Removing one goes through a quarantine step tied to an issue, which a nightly flaky-test hunt opens.

  • scripts/flake.ts holds the rules: the run report, the text of the issue the hunt opens, and the edit that moves a file from GATE to EXCLUDED.
  • scripts/flake-hunt.ts is the command line: admit <file> runs the file ten times and prints a report to paste into the pull, and quarantine moves a flaky file out of the gate under its issue.
  • .forgejo/workflows/flake-hunt.yml runs the hunt every night and opens a Flaky: test/… issue for any file that fails. The issue lists the usual causes and says a retry is not a fix.
  • test/flake.test.ts covers all of this and is in the gate itself. scripts/gate.ts, README.md and CLAUDE.md describe the new admission rule.

Merge with main:

  • test/spawn-first-boot.test.ts conflicted only in a comment: both branches had already raised the median bound to 500 ms, so I kept main's version.
  • Main had added test/plan-route.test.ts without listing it in GATE or EXCLUDED, which made the gate audit in check-list.test.ts and flake.test.ts fail. That test timed out at 30 s on each of three runs here, far over the 1 s budget for a test. I listed it in EXCLUDED as "not yet admitted" under #261. Making it fast is left to that issue.

flake.test.ts, check-list.test.ts and spawn-first-boot.test.ts pass after the merge. I couldn't fully run two other checks here:

  • Type check: it stopped on a missing bun type definition before checking any code.
  • admit on plan-route.test.ts: it printed nothing before my 280 s limit cut it off.

To try it: bun scripts/flake-hunt.ts admit test/flake.test.ts, then bun test test/flake.test.ts test/check-list.test.ts.

🤖 Generated with Claude Code

commented

Parked by the crew: the check stayed red through 2 automatic tries

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs