agentleFS
yaohuangguanyaohuangguan/agent-playbook/skills/fix-flaky-test/SKILL.md

fix-flaky-test

Skill1 starsChanged 2 days agoMITView on GitHub

Find and fix the cause of a test that fails only sometimes, such as on CI, on a slow machine or in a certain order. Use this for any intermittent failure, timeout in a test or "passes on rerun" situation. It replaces retries and longer sleeps with a proven fix.

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 4 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Run the test many times in a loop.

    line 23
  2. Distinctive:

    Run it with other tests, in a different order, or in parallel.

    line 24
  3. Distinctive:

    Add CPU load, or limit the CPU.

    line 25
  4. Distinctive:

    Make sure a timeout prints enough output to explain itself.

    line 44

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Fix a flaky test

A flaky test is a real defect, in the test or in the product. Retrying hides it and teaches everyone to ignore red builds. The aim is to reproduce it on demand, fix the cause, and show that the same conditions no longer break it.

When to use

A test fails intermittently, or passes when rerun without changes.

Steps

  1. Collect the failures. Save the exact message, OS, job and commit. Note what the failing runs have in common.
  2. Reproduce it. Try these until it fails:

    • Run the test many times in a loop.
    • Run it with other tests, in a different order, or in parallel.
    • Add CPU load, or limit the CPU.
    • Slow down the code it waits for with a temporary delay.

    Slowing the code is often the quickest way to prove a timing bug.

  3. Classify the cause.

    TypeSignsUsual fix
    Fixed wait too shortFails on slow hostsWait for the outcome
    Shared stateDepends on test orderGive each test its own state
    Real clockFails near midnight or a boundaryInject the clock
    Unordered resultsOrder changes between runsSort or compare as a set
    Port, file or process conflictFails in parallelUse unique resources
    Real race in the productWrong result under loadFix the product
    External dependencyFails with the networkStub the boundary
  4. Fix the cause. Replace a fixed wait with a check on the actual outcome. A deadline only catches a hang, so set it longer than the time the product allows. Make sure a timeout prints enough output to explain itself.
  5. Check the assertions are unchanged. The fix must not make the test accept behaviour it used to reject.
  6. Prove the fix. Repeat the conditions from step 2. The old code must fail and the new code must pass under the same stress.
  7. Finish. Follow verify-and-report.

Stop and ask when

  • You cannot reproduce it after trying everything in step 2. Report what you tried, and add diagnostics instead of guessing.
  • The cause is a real product race that needs a design change.

Common mistakes

Tempting moveWhy it failsDo this instead
Add a retryHides the defectFind the cause
Increase a sleepSlower, and still flakyWait for the outcome
Mark it as skippedRemoves the protectionFix it, or file a linked issue with the owner's approval
Loosen the assertionAccepts wrong behaviourKeep the assertion
Declare it fixed after one passOne pass proves nothingRepeat under stress

Example

Failure: replaced=false after 5 seconds, on Windows runners only.

Weak fix: raise the wait from 5 to 15 seconds.

Strong fix:

Reproduce: slowed the old process's shutdown to 6 seconds, and the test failed
with the same message as CI.
Cause: the test stopped after a fixed 5 seconds, while the handoff can take longer.
Fix: wait until the handoff finishes, with a 60-second bound for hangs.
Proof: with the same 6-second delay, the old test fails and the new one passes.
Assertions: unchanged.

Report

Failure: <message, where it occurred>
Reproduced by: <command or condition>
Cause: <type and explanation>
Fix: <what changed>
Proof: <old fails, new passes, under the same condition>
Assertions changed: <none, or why>

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.