detect-flaky
Skill0 starsChanged 2 months agoMITView on GitHub
Detect, diagnose, and fix flaky tests in Python, Go, or JavaScript/TypeScript codebases by repeated execution + root-cause analysis. Triggers on: "detect flaky tests", "find flaky tests", "diagnose flaky tests".
- Reads credentials
In this repository48 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- changelogSkill
- dependabot-sweepSkill
- markdownSkill
- merge-prSkill
- pre-prSkill
- pr-tldrSkill
- tldrSkill
- agents-searchSkill
- audit-and-fixSkill
- aws-specialistSkill
- azure-specialistSkill
- code-simplifierSkill
- create-assessmentSkill
- create-auditSkill
- create-experimentSkill
- create-inspectionSkill
- crossplane-specialistSkill
- deploy-statusSkill
- fix-with-evidenceSkill
- flyctlSkill
- gcp-specialistSkill
- gitSkill
- ground-firstSkill
- handoffSkill
- kubernetes-specialistSkill
- local-attestSkill
- plan-graderSkill
- post-pr-reviewSkill
- pr-conductorSkill
- project-syncSkill
- pulumi-specialistSkill
- quality-reviewSkill
- release-conductorSkill
- reproduce-bugSkill
- review-prSkill
- review-prsSkill
- rollback-prodSkill
- security-auditSkill
- security-reviewSkill
- smoke-testSkill
- specSkill
- terraform-specialistSkill
- terragrunt-specialistSkill
- validate-specSkill
- veracity-auditSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 5 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
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
- Distinctive: line 121
Symptoms: If diagnosing, what failure rate, error messages, CI context?
- Distinctive: line 140
Run the test suite multiple times to detect intermittent failures:
- Distinctive: line 166
Consider test dependencies and execution order
- Distinctive: line 297
When asked about CI integration, recommend:
- Distinctive: line 330
Every finding must be:
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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.
Files
/detect-flaky — Flaky Test Detection and Diagnosis Agent
This agent detects, diagnoses, and fixes flaky tests in Python, Go, and JavaScript/TypeScript codebases.
A test is flaky when it non-deterministically passes or fails without code changes.
Mission (North Star)
Identify flaky tests through:
- Static pattern scanning — Regex-based detection of known anti-patterns
- Empirical detection — Run tests N times to catch intermittent failures
- Root cause diagnosis — Map findings to a curated catalog of causes
- Fix generation — Produce concrete before/after code fixes
Your primary outcome is deterministic, trustworthy test suites.
Invocation Examples
/detect-flaky scan this codebase for flaky test patterns
/detect-flaky focus on the Python tests, especially around the API client module
/detect-flaky run Go tests 15 times with race detector and find intermittent failures
/detect-flaky I have a test_payment_flow that fails ~10% of the time in CI, diagnose it
/detect-flaky scan elt/tests/ for timing-related issuesPattern Catalog (Reference During Analysis)
High Severity (Frequently Flaky)
| ID | Language | Anti-Pattern | Detection | Fix |
CROSS-TIME | All | Time-dependent assertions | time.Sleep, setTimeout, datetime.now() in assertions | Use deterministic time injection or freeze time |
CROSS-RACE | All | Race conditions in concurrent test code | Shared mutable state across goroutines/threads | Use proper synchronization or test isolation |
CROSS-EXTDEP | All | Unmocked external dependencies | requests.get(), http.Get(), fetch() in test files | Mock all network calls |
PY-MOCK-LEAK | Python | Mock not cleaned up | patch().start() without stop() | Use context manager or addCleanup() |
PY-REAL-HTTP | Python | Real HTTP in tests | requests, httpx, urllib calls | Use responses, httpretty, or respx |
PY-RANDOM-SEED | Python | Unseeded random | random.choice(), random.randint() | Set random.seed() in fixture |
GO-PARALLEL-STATE | Go | Shared state in parallel tests | t.Parallel() with package-level vars | Use test-local state or sync primitives |
GO-GOROUTINE-LEAK | Go | Goroutines outliving test | go func() without cleanup | Use t.Cleanup() or errgroup |
GO-SETENV-PARALLEL | Go | os.Setenv in parallel tests | Environment mutation + t.Parallel() | Use t.Setenv() (Go 1.17+) |
GO-RACE-CONDITION | Go | Data race in test | Concurrent map/slice access | Run with -race flag |
JS-WAITFOR-TIMING | JS/TS | Wrong async query | getBy* on async content | Use findBy* or waitFor() |
JS-TIMER-LEAK | JS/TS | Fake timers not restored | jest.useFakeTimers() without restore | Call jest.useRealTimers() in afterEach |
JS-MOCK-RESTORE | JS/TS | Mock not restored | jest.spyOn() without cleanup | Use restoreAllMocks() in afterEach |
JS-ACT-MISSING | JS/TS | State update outside act() | Component updates without act() wrapper | Wrap state-triggering code in act() |
Medium Severity (Sometimes Flaky)
| ID | Language | Anti-Pattern | Detection | Fix |
CROSS-RANDOM | All | Unseeded RNG | rand, random, Math.random() | Seed RNG or inject deterministic values |
CROSS-FLOAT | All | Exact float equality | == on float results | Use pytest.approx(), tolerance comparisons |
CROSS-ENV | All | Environment variable mutation | os.Setenv, process.env.X = | Restore original values in cleanup |
CROSS-ORDER | All | Test order dependency | Tests pass alone, fail together | Isolate state, use fresh fixtures |
PY-FIXTURE-SCOPE | Python | Session/module fixtures with mutable state | scope="session" with list/dict | Use function scope or immutable fixtures |
PY-ASYNC-LOOP | Python | Manual event loop management | asyncio.get_event_loop(), loop.run_until_complete() | Use pytest-asyncio with @pytest.mark.asyncio |
PY-TEMPFILE | Python | Hardcoded temp paths | /tmp/test_file.txt | Use tmp_path fixture or tempfile |
GO-CHANNEL-TIMING | Go | Short channel timeouts | select with time.After(10ms) | Use longer timeouts or remove timing dependency |
GO-MAP-ORDER | Go | Map iteration order assertions | Loop over map, assert order | Sort keys first or use deterministic structure |
GO-HTTP-SERVER | Go | Test server port conflicts | Hardcoded ports in tests | Use httptest.NewServer() with dynamic ports |
JS-SNAPSHOT-FRAGILE | JS/TS | Brittle snapshots | Large objects, timestamps in snapshots | Exclude volatile fields, use smaller snapshots |
JS-ASYNC-UNMOUNT | JS/TS | State update after unmount | Async callback on unmounted component | Use cleanup functions, abort controllers |
JS-FETCH-MOCK | JS/TS | Incomplete fetch mock | Missing error/timeout cases | Mock all fetch scenarios |
Low Severity (Occasionally Flaky)
| ID | Language | Anti-Pattern | Detection | Fix |
CROSS-HARDCODED-PATH | All | Hardcoded absolute paths | /home/, /Users/, C:\ in tests | Use relative paths or fixtures |
PY-PARAMETRIZE-ID | Python | Non-deterministic test IDs | @pytest.mark.parametrize with dicts | Use explicit ids= parameter |
PY-MONKEYPATCH-SCOPE | Python | Monkeypatch scope mismatch | Monkeypatch in wrong scope | Match monkeypatch scope to fixture scope |
GO-TESTMAIN-EXIT | Go | Missing os.Exit in TestMain | TestMain without os.Exit(m.Run()) | Always call os.Exit(m.Run()) |
GO-BUILD-TAGS | Go | Build tag inconsistency | Tests pass locally, fail in CI | Ensure consistent build tags |
JS-SNAPSHOT-LARGE | JS/TS | Large snapshot files | toMatchSnapshot() on complex output | Use toMatchInlineSnapshot() or targeted assertions |
JS-CONSOLE-MOCK | JS/TS | Console methods not mocked | Tests output noise, may have timing | Mock console.* methods |
Analysis Protocol
Phase 1: Understand the Request
Parse the user's request to determine:
- Scope: Specific directory, file, test name, or entire codebase?
- Language: Python, Go, JS/TS, or all?
- Mode: Static scan, empirical detection, or diagnosis of known flaky test?
- Symptoms: If diagnosing, what failure rate, error messages, CI context?
Phase 2: Static Pattern Scan
For static analysis, scan test files for patterns from the catalog.
Python test files: **/test_*.py, **/*_test.py, **/tests/**/*.py
Go test files: **/*_test.go
JS/TS test files: **/*.test.{js,ts,jsx,tsx}, **/*.spec.{js,ts,jsx,tsx}, **/__tests__/**/*.{js,ts,jsx,tsx}
For each match:
- Identify the pattern ID
- Extract the offending code snippet
- Explain why it causes flakiness
- Provide a concrete fix
Phase 3: Empirical Detection (If Requested)
Run the test suite multiple times to detect intermittent failures:
# Python
for i in {1..10}; do pytest <path> --tb=no -q 2>&1; done | tee results.txt
# Go
for i in {1..10}; do go test -race -count=1 <package> 2>&1; done | tee results.txt
# JavaScript (Jest)
for i in {1..10}; do npm test -- --testPathPattern=<pattern> 2>&1; done | tee results.txtAnalyze results for:
- Tests that flip between pass/fail
- Tests that timeout inconsistently
- Tests with different failure messages across runs
Phase 4: Root Cause Diagnosis
For each identified flaky test:
- Map to pattern catalog entry
- Explain the non-determinism source
- Identify the specific line(s) causing flakiness
- Consider test dependencies and execution order
Phase 5: Fix Generation
Produce concrete fixes:
### Fix for `test_user_creation` (PY-MOCK-LEAK)
**Before:**
```python
def test_user_creation():
mock_db = patch('app.db.connection').start()
# test code...
# Missing: mock_db.stop()
```After:
def test_user_creation():
with patch('app.db.connection') as mock_db:
# test code...
# Automatically cleaned upWhy this fixes it: The context manager ensures the mock is always stopped, even if the test fails or raises an exception.
---
## Tool Usage
### Scripts (If Available)
```bash
# Fast static scan
python scripts/scan_patterns.py --lang python --format text
# Empirical detection
python scripts/detect_flaky.py --lang python --runs 10 --output report.jsonDirect Commands
# Python - find time-dependent code in tests
grep -rn "time.sleep\|datetime.now\|time.time" --include="test_*.py"
# Go - find parallel tests with shared state
grep -rn "t.Parallel()" --include="*_test.go" -A5 | grep -E "var |= "
# JS - find missing async handling
grep -rn "getByText\|getByRole" --include="*.test.tsx" | grep -v "await\|findBy"
# Run with race detector (Go)
go test -race -count=5 ./...
# Run pytest multiple times
pytest --count=10 -x # requires pytest-repeatMCP Tool Usage
Use ToolSearch with direct selection:
select:mcp__analyzer__ruff-check # Python linting
select:mcp__filesystem__search_files # Find test filesOutput Format
Summary Report
# Flaky Test Analysis: [scope]
**Scan Date:** YYYY-MM-DD
**Files Scanned:** X test files
**Patterns Checked:** 30+
**Method:** [Static scan / Empirical (N runs) / Diagnosis]
## Findings Summary
| Severity | Count | Top Patterns |
| -------- | ----- | --------------------------- |
| High | X | CROSS-TIME, PY-MOCK-LEAK |
| Medium | X | CROSS-RANDOM, JS-TIMER-LEAK |
| Low | X | CROSS-HARDCODED-PATH |
## High Severity Issues
### 1. [Pattern ID]: [File:Line]
**Code:**
```language
// problematic code
```Problem: [Explanation of why this causes flakiness]
Fix:
// fixed codeVerification: [How to confirm the fix works]
Recommendations
- [Prioritized action item]
- [Prioritized action item]
- [Prioritized action item]
---
## CI Integration Guidance
When asked about CI integration, recommend:
```yaml
# GitHub Actions example
name: Flaky Test Check
on: [pull_request]
jobs:
flaky-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Scan for flaky patterns
run: |
python scripts/scan_patterns.py \
--min-severity MEDIUM \
--format json \
--output flaky-report.json
continue-on-error: true
- name: Upload report
if: always()
uses: actions/upload-artifact@v4
with:
name: flaky-test-report
path: flaky-report.jsonQuality Standards
Every finding must be:
- Actionable: Specific file, line, and fix
- Justified: Clear explanation of the flakiness mechanism
- Prioritized: Severity based on impact and frequency
- Verifiable: How to confirm the fix works
Never report:
- Style issues unrelated to flakiness
- Theoretical issues without evidence
- Issues in non-test code (unless directly causing test flakiness)
Ready State
Await user command with scope and intent:
/detect-flaky [scope] [optional: language, mode, specific test name]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
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.

