Teaching an AI Agent to Debug Flaky Tests
Igor Kulakov ยท article ยท 2026-05-09 source โ Reader ๐ฅ๏ธ
Flaky tests can be debugged deterministically by diffing code-coverage reports from passing and failing runs to locate where execution paths diverge, and this entire procedure can be packaged as a reusable AI Agent Skill so that an agent follows it without human guidance.
practical ยท tutorial-narrative โ A walkthrough that builds from intuition to tooling to a reusable AI Skill, using a concrete TOCTOU race condition as the running example. Part tutorial, part methodology argument.
Structure
| Part | Role |
|---|---|
| Problem statement: flaky tests undermine CI | Establishes the pain โ non-deterministic pass/fail with no code changes wastes human and infrastructure resources |
| Example project: TOCTOU race in a Spring Boot webshop | Provides a concrete, reproducible flaky test (concurrent checkout producing duplicate invoice numbers) |
| The intuition: different results imply different code paths | Derives the key insight โ if we record and diff the code paths of passing vs. failing runs, the divergence point reveals the root cause |
| Building the tools: coverage agent + text reporter + diff script | Shows how to (1) run IntelliJ coverage from CLI with hit counting, (2) convert binary reports to text, (3) collect multiple runs and summarize varying lines |
| Interpreting the diff: hit-count variation pinpoints the race | Demonstrates that the varying lines point exactly to the lazy-init block where the TOCTOU race lives |
| Packaging as an AI Agent Skill | Converts the manual procedure into a SKILL.md that an agent can discover and execute autonomously |
| Testing the Skill in a fresh agent session | Validates that a naive prompt ('find and fix the cause of flakiness') triggers skill discovery and correct execution |
Key terms
- AI Agent Skill Agent Skills
A plain-text document (SKILL.md) in a discoverable location that describes a reusable procedure, conventions, or domain knowledge an AI agent can follow โ the AI equivalent of a knowledge base article - flaky test
A test that returns both passes and failures despite no changes to code or test - coverage diff
A comparison of per-line hit counts from coverage reports across multiple test runs, summarizing lines whose execution counts vary between passing and failing runs - hit counting
Recording the number of times each line is executed (not just whether it was hit), enabling detection of concurrency bugs where the same code runs more times than expected - TOCTOU (time-of-check to time-of-use)
A race condition where a thread checks a condition and then acts on it, but another thread changes the state between check and use
Propositions
- explicitFlaky tests must take different code paths on passing vs. failing runs; otherwise results would be consistent.conf: high
- explicitTest coverage tools, though not designed for tracing, can record the code-path information needed to diagnose flakiness.conf: high
- explicitHit counting (execution counts per line) is essential โ some flaky bugs differ not in which lines execute but in how many times they execute.conf: high
- explicitcorroboratedDiffing coverage reports from passing and failing runs can deterministically point to the root cause of flakiness.conf: mediumsupports โOpenAI Fixes 18-Year-Old GNU libunwind Bug by Treating Crash Debugging Like Epidemiology ยท p1 โ Parallel instance of one principle โ comparative/differential data across many cases localizes root cause where single-case inspection fails. There: diffing passing-vs-failing coverage reports. Here: labeling and correlating a year-long population of core dumps to split one syndrome into two clusters.
- explicitAI Agent Skills make hard debugging procedures accessible to agents that have no prior context about the problem.conf: high
- implicitThe coverage-diff technique generalizes beyond TOCTOU races to other classes of flaky tests.conf: medium
- explicitText interfaces are the composable building blocks that allow AI agents to solve complex problems โ as long as you can stack text interfaces together, you can teach agents to solve virtually anything.conf: medium
Arguments
Coverage-diff is a sound diagnostic for flaky tests strength: medium
Explicit premises:
- P1: Flaky tests must take different code paths
- P2: Coverage tools can record code-path information
- P3: Hit counting captures frequency differences, not just presence/absence
Implicit premises:
- The code-path differences that cause flakiness are observable at the line/branch level of granularity
Inferences:
- If flakiness implies path divergence, and coverage captures paths, then coverage-diff reveals the divergence point
Packaging the procedure as an AI Skill makes debugging deterministic strength: medium
Explicit premises:
- The coverage-diff procedure is mechanical and repeatable
- AI Skills encode procedures so agents follow them step-by-step
Implicit premises:
- Determinism here means the agent consistently arrives at the correct diagnosis, not that the model output is literally identical
Inferences:
- Deterministic procedure + faithful execution = deterministic outcome
Narratives
- From intuition to tool to Skill: the article narrates a single debugging session that progresses from a vague hunch ('different results must mean different paths') through tool-building (coverage agent, text reporter, diff script) to a validated, reusable AI Skill.
โ Demonstrates the methodology by example and implicitly argues that AI-augmented developers should package their insights into reusable Skills
Questions addressed
- โ
Can we make AI deterministically find the root cause of flaky tests?
Yes โ by encoding a coverage-diff procedure as an AI Skill, the agent follows deterministic steps to locate the divergence point - โ
What tool can record code paths for comparison between passing and failing test runs?
IntelliJ IDEA's coverage agent with hit counting, run via CLI through Maven Surefire - โ
How can we convert binary coverage data into a format useful for AI analysis?
A custom TextCoverageStatistics reporter was added to the open-source IntelliJ coverage agent - โ
Can the entire flaky-test debugging procedure be encoded as a reusable AI Agent Skill?
Yes โ packaged as SKILL.md following the agentskills.io specification; validated in a fresh agent session
Gaps
- evidentialOnly one bug class demonstrated โ the TOCTOU race condition. No evidence the technique works for other common flakiness causes: environment-dependent tests, time-sensitive tests, test-order dependencies, resource leaks, or non-deterministic external services.
- conceptualThe claim of 'deterministic' debugging conflates procedural repeatability with outcome reliability. The agent's interpretation of the coverage diff is still probabilistic, and the article does not measure how often the agent correctly identifies root causes across varied bugs.
- practicalThe technique requires the flaky test to actually fail during the collection runs. For tests that fail once in thousands of runs, the script's loop-until-both-outcomes approach may be impractical without guidance on how many runs are needed.
- conceptualAssumes line-level coverage granularity is sufficient. Some flakiness may involve identical line-level execution with different data values, timing, or ordering that coverage cannot capture.
Related documents
- Have your agent record video demos of its work with shot-scraper video
- Agentic Engine Optimization (AEO)
- With Android CLI, Google is Making the Android Toolchain Agent-Friendly
- Agent Skills: Google-Backed AI Coding Agent Workflow
- Agent Skills
- This Week's Best Developer Tools & Betas
- GitHub Copilot CLI Reaches General Availability
- Kafka App? There's a Skill for That
- GitHub Copilot CLI Gets Tabs and No-Config-File Tool Setup in Redesigned Terminal UI
- 10 Things Developers Want from their Agentic IDEs in 2025